# vue3-three.js **Repository Path**: study-three-js/vue3-three.js ## Basic Information - **Project Name**: vue3-three.js - **Description**: 学习b站老陈打码 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-02-09 - **Last Updated**: 2023-05-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # vue 3-three.js 学习b站老陈打码 ## Vue 3 + Vite This template should help get you started developing with Vue 3 in Vite. The template uses Vue 3 ` ``` #### 子向父传参 > 子组件代码 ```vue ``` > 父组件接收代码 ```vue ``` #### 去掉Eslint 警告 ![image.png](README_files/1.png) ```vue ``` ## Vue 3路由 ### 安装路由 ```shell $ npm install vue-router@4 ``` ### 创建router文件夹里面新建index.js ```javascript // 1.导入路由 /** * 创建路由方法 createRouter * * 创建不同的历史模式 createWebHashHistory,createWebHistory 用其一即可 * Hash 模式 createWebHashHistory 在内部传递的实际 URL 之前使用了一个哈希字符(#) * HTML5 模式 createWebHistory 创建 HTML5 模式 */ import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router' // 2.配置页面 /** * 不写后缀需要vite.config.js里面配置 */ import Home from '../views/Home.vue' import About from '../views/About.vue' import Buycart from '../views/Buycart.vue' // 3.定义路由 /** * 每个路由都需要映射到一个页面/组件 * * 嵌套路由稍后补充 */ const routes = [ // { path: '/', components: Home, name: 'home', }, { path: '', component: Home, name: 'home', }, { path: '/about', component: About, name: 'about', }, { path: '/buycart', component: Buycart, name: 'buycart', }, ] // 4.创建路由实例并传递 'router' 配置(先简单配置一下) const router = createRouter({ // 5.内部默认提供了history 模式的实现。为了简单起见,暂时这里用 hash模式 history: createWebHashHistory(), routes, // router:router 的 es6缩写 }) export default router ``` #### 不同的历史模式 ##### Hash 模式 > URL中带有"#"号。 > 例:[http://localhost:8080/#/login](http://localhost:8080/#/login) > > hash模式的特点 > > hash模式下,前端路由修改的是#号中的信息,对后端完全没有影响,因此改变hash也不会重新加载整个页面。如果修改不存在的#abc页面,页面也不会跳转,history模式则刚好相反,没有对应的页面就会出现404。 ##### history模式 > URL中没有"#"号。 > 例:[http://localhost:8080/login](http://localhost:8080/login) > > history模式的特点 > > history模式下,操作中不怕前进和后退,不带#号。它的缺点是害怕刷新页面,如果没有服务器端的支持,刷新之后就会请求服务器,由于找不到相应的支持响应或者资源,就会报错404页面。 #### 浏览器报错 ---Vue Router4特有的 > vue-router.esm-bundler.js:3308 Error: Invalid route component **如果在配置路由当中的path有为空的把斜杠‘/’去掉,换成空字符** ### 在main.js中去引入router下的文件 ```javascript import { createApp } from 'vue' import App from './App.vue' // 6.导入路由 import router from './router' // createApp(App).mount('#app') //拆开写 let app = createApp(App) // app.mount('#app') // 7. 安装路由 app.use(router) app.mount('#app') ``` ### 在App.vue中去使用路由 > 需要配合 `router-link` 和 `router-view`组件 > > 和Vue2一致 ```vue ``` ## Vue 3 Vuex 状态管理 ### 安装 Vuex ```shell $ npm install vuex@next --save ``` ### 创建store文件夹里面新建index.js ```javascript /** * Vuex4.0 以前都是Vue2的,以后才是Vue3的 */ // 1.导入Vuex import { createStore } from 'vuex' // 2.创建一个新的 store 实例 const store = createStore({ state() { return { count: 0, } }, // 计算属性 获取一个新的值 getters: { totalPrice(state) { return state.count * 98.8; }, }, // 同步修改状态的方法 mutations: { increment(state, payload) { // payload 是传过来的参数 state.count += payload; }, }, // 异步修改状态的方法(调接口的时候) actions: { asyncAdd(store, payload) { // payload 是传过来的参数 setTimeout(() => { // store.commit('increment', 10) store.commit('increment', payload) }, 1000) }, }, }) // 3.导出store export default store; ``` ### 在main.js中去引入store下的文件 ```javascript import { createApp } from 'vue' import App from './App.vue' // 6.导入路由 import router from './router' // 4.导入Vuex import store from './store' // createApp(App).mount('#app') //拆开写 let app = createApp(App) // app.mount('#app') // 7. 安装路由 app.use(router) // 5. 安装Vuex app.use(store) app.mount('#app') ``` ### 在App.vue中去使用Vuex ```vue ```