Vue3.0-初見

ついにVueの新しいバージョンを試すことに取り掛かりました。この記事では客観的になるつもりはありません。新しいバージョンで作業しているときの感想と、インストールして今すぐ始める方法についても説明します。

リリースにもかかわらず、Vue3.0はまだ完全に本番環境で使用する準備ができていません。RouterとVuexはまだ新しいバージョンで動作する準備ができていません。VueCLIは、作成者がそれらを更新する時間がなかったサードパーティのプラグインとライブラリは言うまでもなく、デフォルトで古いバージョンをインストールします。これは私たちが受け取った待望の不完全なリリースです。

特に、新しい構文、いわゆるコンポジションAPIによって多くの疑問が生じます。これは、幸いなことに、完全に馴染みのある、愛されているオプションAPIに取って代わるものではありません。新しい構成を支持して、私たちはどこでも同様の写真を提供されます:

新旧の構文におけるコードの断片化の比較
新旧の構文におけるコードの断片化の比較

. , , - . , , TS . - , . . .

" " : React. , , , .

.

Vue 3, :

Vue 3

npm - :

npm install vue@next

node_modules. , . Vue CLI.

: CLI. :

npm install -g @vue/cli

- , yarn ( Vue React):

yarn global add @vue/cli

, , . , :

vue -V

@vue/cli 4.5.6.

:

vue create hello-vue

. :

Default (Vue 3 Preview) ([Vue 3] babel, eslint)

, :

cd hello-vue

. VScode :

code  .

Vue CLi . . main.js:

import { createApp } from 'vue'
import App from './App.vue'

createApp(App).mount('#app')

createApp, . . , , , - , . , .

HelloWorld.vue App.vue.

components Card.vue :

<template>
  <div>
    <img :src="imgUrl" alt="cat pic" />
    <h1>{{ catName }}</h1>
    <p>
      <i>{{ text }}</i>
    </p>
    <p>
      <b>{{ website }}</b>
    </p>
  </div>
</template>

<script>
export default {
  name: "Card",
  props: {
    id: String,
    name: String,
    text: String,
    website: String,
  },
  computed: {
    imgUrl() {
      return `https://robohash.org/${this.id}?set=set4&size=180x180`;
    },
    catName() {
      return this.name.replace(/[_.-]/gi, " ");
    },
  },
};
</script>

props. , . , , . computed. ? computed

<script>
import { computed } from "vue";
...

computed setup :

 setup(props) {
    const imgUrl = computed(
      () => `https://robohash.org/${props.id}?set=set4&size=180x180`
    );
    const catName = computed(() => props.name.replace(/[_.-]/g, " "));
    return {
      imgUrl,
      catName,
    };
  },

Setup props, .

, , robohash.org. . :

<style scoped>
div {
  width: 400px;
  height: 380px;
  position: relative;
  background: #ecf0f3;
  margin-bottom: 30px;
  padding: 30px 5px;
  border-radius: 32px;
  text-align: center;
  cursor: pointer;
  font-family: "Montserrat", sans-serif;
  font-size: 22px;
  font-weight: semibold;
  box-shadow: -6px -6px 10px rgba(255, 255, 255, 0.8),
    6px 6px 10px rgba(0, 0, 0, 0.2);
  color: #6f6cde;
}
</style>

App.vue. . :

<template>
  <div class="container">
    <Card
      v-for="cat in cats"
      :id="cat.id"
      :name="cat.username"
      :text="cat.company.catchPhrase"
      :key="cat.id"
      :website="cat.website"
    />
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import Card from "./components/Card";

export default {
  name: "App",
  components: { Card },
  setup() {
    const cats = ref([]);
    const fetchCats = async () => {
      cats.value = await fetch(
        "https://jsonplaceholder.typicode.com/users"
      ).then((response) => response.json());
    };
    onMounted(fetchCats);
    return {
      cats,
    };
  },
};
</script>

<style>
body {
  background: #ecf0f3;
}
.container {
  height: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-content: space-around;
  flex-wrap: wrap;
  padding: 30px 0px;
}
</style>

, :

ref onMounted

ref , setup(). -. ref . , , , . value , , :

cats.value = data

cats

onMounted mounted. setup , on. onMounted . , , .


, .

.

. ( ) . , . .




All Articles