前端开发那些事儿

【Vue3.0】- 入坑 - 全家桶

2020-11-23  本文已影响0人  啦啦啦喽啰

快速搭建 Vue 3.0 项目

版本

"dependencies": {
    "core-js": "^3.6.5",
    "vue": "^3.0.0-0",
    "vue-router": "^4.0.0-0",
    "vuex": "^4.0.0-0"
  }

升级vue-cli

npm install -g @vue/cli
vue -V
 vue/cli@4.5.4

创建项目

vue create vue3-demo

配置项目

目录结构

./src
├── App.vue
├── assets
│   └── logo.png
├── components
│   └── HelloWorld.vue
├── main.js
├── router
│   └── index.js
├── store
│   └── index.js
└── views
    ├── About.vue
    └── Home.vue

开发流程

新建页面

<template>
  <div class="page">Hello</div>
</template>

<script>
export default {

}
</script>

<style lang="scss" scoped>
.page {
  color: red;
}
</style>

创建路由

import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  },
  {
    path: '/hello',
    name: 'Hello',
    component: () => import('../views/Hello.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router

vue-router有哪些改变

import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
const router = createRouter({
  history: createWebHistory('/'),
  ...
})
// 当路由为 /user/a/b 时,捕获到的 params 为 {"a": "a", "catchAll": "/b"}。
const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/user/:a:catchAll(.*)', component: component },
  ],
})
app.use(router)
// Note: on Server Side, you need to manually push the initial location
router.isReady().then(() => app.mount('#app'))

状态和事件绑定

<template>
  <div class="page">
    <div>Hello</div>
    <div>{{count}}</div>
    <button @click="add">ADD</button>
  </div>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const count = ref(0);
    const add = () => {
      count.value++
    }
    return {
      count,
      add
    }
  }
}
</script>

计算属性和监听器

<template>
  <div class="page">
    <div>Hello</div>
    <div>{{count}}</div>
    <div>double: {{doubleCount}}</div>
    <button @click="add">ADD</button>
  </div>
</template>

<script>
import { ref, computed, watch } from 'vue';

export default {
  setup() {
    const count = ref(0);
    const doubleCount = computed(() => count.value * 2)
    const add = () => {
      count.value++
    }
    watch(() => count.value, val => {
      console.log(`count is ${val}`)
    })
    return {
      count,
      doubleCount,
      add
    }
  }
}
</script>

获取当前路由

import { getCurrentInstance } from 'vue';
// 获取当前实例
const { ctx } = getCurrentInstance();
// 获取当前路由
console.log(ctx.$router.currentRoute.value);

页面跳转

import { useRouter } from 'vue-router';
export default {
  setup() {
    ...
    const router = useRouter();
    const backHome = () => {
      router.push('/')
    }
    ...
  }
}

Vuex 集成

定义 Vuex 状态

import { createStore } from 'vuex'

export default createStore({
  state: {
    name: 'haha'
  },
  mutations: {
    setHelloName(state, value) {
      state.name = value;
    }
  },
  actions: {
  },
  modules: {
  }
})

获取 Vuex 状态

import { getCurrentInstance } from 'vue';
// 获取当前实例
const { ctx } = getCurrentInstance();
// vuex state
const name = computed(() => ctx.$store.state.name);

更新 Vuex 状态

export default {
  setup() {
    // 获取当前实例
    const { ctx } = getCurrentInstance();
    // vuex state
    const name = computed(() => ctx.$store.state.name);
    const update = () => {
      ctx.$store.commit('setHelloName', 'haha -- ' + ctx.count );
    }
    ...
  }
}
上一篇下一篇

猜你喜欢

热点阅读