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

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設(shè)置
在src下新建router目錄,創(chuàng)建index.js,文件內(nèi)容如下
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設(shè)置
在src下新建store目錄,創(chuàng)建index.js,文件內(nèi)容如下
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倉庫



這樣就是成功了

到此這篇關(guān)于Vue項目依賴包安裝及配置的文章就介紹到這了,更多相關(guān)vue依賴包安裝配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue?cli2?和?cli3去掉eslint檢查器報錯的解決
這篇文章主要介紹了vue?cli2?和?cli3去掉eslint檢查器報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue3項目中配置TypeScript和JavaScript的兼容
在Vue3開發(fā)中,常見的使用JavaScript(JS)編寫代碼,但也會有調(diào)整編寫語言使用TypeScript(TS)的需求,因此,在Vue3項目設(shè)置中兼容TS和JS是刻不容緩的重要任務(wù),2023-08-08
使用 vue 實例更好的監(jiān)聽事件及vue實例的方法
這篇文章主要介紹了使用 vue 實例更好的監(jiān)聽事件及vue實例的方法,介紹了一種新增 vue 實例的方法,單獨監(jiān)聽事件,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
vue組件屬性(props)及私有數(shù)據(jù)data解析
這篇文章主要介紹了vue組件屬性(props)及私有數(shù)據(jù)data解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

