Vue項目依賴包安裝及配置過程
1.安裝axios ,router,vue-router,vuex (npm i xxx -S)
注意依賴包對應版本

2. elementui 按需引入配置
npm i element-ui -S
npm install babel-plugin-component -D
babel.config.js文件添加配置
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
],// 這個不修改
"plugins": [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
}
main.js
import {Button, Select} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(Button)
Vue.use(Select)不要把組件注冊寫成對象格式:Vue.use({Button,Select}),無法正常注冊

3. router設置
在src下新建router目錄,創(chuàng)建index.js,文件內容如下
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
export default new VueRouter({
routes:[
{
name:'歡迎頁',
path:'/hello',
component:()=>import('@/components/HelloWorld')
}
]
})在main.js注冊路由

使用路由

4.vuex設置
在src下新建store目錄,創(chuàng)建index.js,文件內容如下
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store( {
namespaced:true,//開啟命名空間,避免沖突
// 提供唯一的公共資源,所以共享的數(shù)據(jù)統(tǒng)一放到store進行儲存,類似data
state:{
testID:'123456'
},
getters:{},
mutations:{},
actions:{},
modules:{}
})在main.js入口文件中注冊store

在組件里使用testID,出現(xiàn)報錯"$store" is not defined on the instance but referenced during render...


查看package.json發(fā)現(xiàn)vuex版本不匹配

vuex現(xiàn)在默認vuex4版本,vuex4只能在vue3中使用,在vue2中能使用vuex3,當前項目使用vue2
刪除node_modules目錄下的vuex文件夾及package.json的vuex,重新安裝vuex@3
npm install vuex@3 -S

5.axios配置
import Axios from "axios";
const instance = Axios.create({
baseUrl: "/api",
});
instance.interceptors.request.use(
(config) => {
// console.log(config);
return config;
},
(err) => {
return Promise.reject(err);
}
);
instance.interceptors.response.use(
(res) => {
return res.data;
},
(err) => {
return Promise.reject(err);
}
);
export default instance;
6.項目上傳到git倉庫



這樣就是成功了

到此這篇關于Vue項目依賴包安裝及配置的文章就介紹到這了,更多相關vue依賴包安裝配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue項目使用Websocket大文件FileReader()切片上傳實例
這篇文章主要介紹了Vue項目使用Websocket大文件FileReader()切片上傳實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
vue中使用unity3D如何實現(xiàn)webGL將要呈現(xiàn)的效果
這篇文章主要介紹了vue中使用unity3D如何實現(xiàn)webGL將要呈現(xiàn)的效果,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

