<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Vue+element搭建后臺管理系統(tǒng)-二、安裝插件

          共 4174字,需瀏覽 9分鐘

           ·

          2022-08-08 07:36

          導讀

          我們繼續(xù)上一章的內容,上一章講到我們已經能將項目成功跑起來了,那么我們接下來把項目必用的東西完善一下。

          01

          安裝ElementUI

          終于到了我們的主角了,在VSCode中新建一個終端,然后通過這個命令來安裝:

          npm i element-ui -S

          至于為什么要-S呢?即--save(保存)包名會被注冊在package.json的dependencies里面,在生產環(huán)境下這個包的依賴依然存在。

          安裝完成之后呢,我們要通過導入才能在項目中使用,可以在main.js中做全局引用:

          import Vue from 'vue'import App from './App.vue'//引入elementUIimport ElementUI from 'element-ui';import 'element-ui/lib/theme-chalk/index.css'; //樣式文件一定要引入
          Vue.config.productionTip = false
          Vue.use(ElementUI)new Vue({ render: h => h(App),}).$mount('#app')

          這樣就可以做到全局引入,如果在實際開發(fā)中用到UI框架的插件沒有很多,也是可以通過按需引入的,下面來說說如何按需引入:

          import { Message} from 'element-ui';Vue.use(Message)

          上面就是引入一個Message彈窗的功能,也就是element中的內容只有這個可以用,還是覺得挺麻煩的哈。

          好了,導入和裝載完畢之后,我們測試一下看看有沒有成功。

          在App.vue文件中加入button組件,然后保存查看,可以看到網(wǎng)頁中已經成功渲染按鈕組件了。

          <template>  <div id="app">    <img alt="Vue logo" src="./assets/logo.png">    <el-button type="primary">測試按鈕</el-button>    <HelloWorld msg="Welcome to Your Vue.js App"/>  </div></template>

          2c331fc7281e19c8a05a11c8eeb12674.webp

          02

          安裝路由

          由于Vue在開發(fā)時對路由支持的不足,于是官方補充了vue-router插件。

          Vue的單頁面應用是基于路由和組件的,路由用于設定訪問路徑,并將路徑和組件映射起來。

          在終端中通過這個命令安裝:

          npm install vue-router -S

          安裝完成之后,同樣在main.js中掛載它。我們項目src的目錄下創(chuàng)建一個router文件夾,用于存放路由映射文件。

          在router文件夾下創(chuàng)建index.js和routers.js,分別用于初始化路由和配置路由映射。index.js代碼如下:

          import Vue from 'vue';import Router from 'vue-router';import constantRoutes from './routers';

          const originalPush = Router.prototype.push;Router.prototype.push = function (location) { return originalPush.call(this, location).catch(err => err);};
          Vue.use(Router);
          const createRouter = () => new Router({ scrollBehavior: () => ({ y: 0 }), routes: constantRoutes})
          const router = createRouter()

          export function resetRouter() { const newRouter = createRouter() router.matcher = newRouter.matcher}
          /** * 全局前置守衛(wèi) * @func beforeEach * @param {object} to 即將要進入的目標 路由對象 * @param {object} form 當前導航正要離開的路由 * @func next 進行管道中的下一個鉤子 */router.beforeEach(async (to, form, next) => {});

          /** * 全局后置鉤子 * @func afterEach * @param {object} to 即將要進入的目標 路由對象 * @param {object} form 當前導航正要離開的路由 */router.afterEach((to, form) => { });

          export default router;

          routers.js代碼如下:

          /** * 逐個導出模塊 */export const constantRoutes = [    {        path: '/',        redirect: '/home'    },]
          export default [ ...constantRoutes,];

          然后在main.js中做好配置:

          import Vue from 'vue'import App from './App.vue'//引入elementUIimport ElementUI from 'element-ui';import 'element-ui/lib/theme-chalk/index.css'; //樣式文件一定要引入
          //載入路由import router from './router/index.js';
          Vue.config.productionTip = false
          Vue.use(ElementUI)new Vue({ router, render: h => h(App),}).$mount('#app')

          保存之后,可能會報ESLint校驗規(guī)則的錯:

          d1f36c93b479b3aab06914714e93f037.webp

          我們先不配置代碼校驗規(guī)則先,后面我們再單獨講代碼編寫規(guī)范。

          去掉代碼校驗的話,在package.json文件的eslintConfig字段中,加入這些代碼,然后重啟項目,就可以了。

          "rules": {            "generator-star-spacing": "off",            "no-tabs": "off",            "no-unused-vars": "off",            "no-console": "off",            "no-irregular-whitespace": "off",            "no-debugger": "off"        }

          7b4b15c3fda6e6b694082a8a372db3f7.webp

          然后我們的路由安裝就算完成了。

          03

          安裝Vuex

          在開發(fā)大型項目的過程中,還是會常常用到vuex的,關于vuex官方的解釋是:vuex是專門用來管理vue.js應用程序中狀態(tài)的一個插件。他的作用是將應用中的所有狀態(tài)都放在一起,集中式來管理。描述可能會有些晦澀難懂,不理解的同學,我們邊用邊學。

          在終端中通過這個命令來安裝:

          npm install vuex --S

          靜靜等待安裝完成后,我們將它裝載在Vue中,步驟跟裝載路由差不多,現(xiàn)在src目錄下創(chuàng)建store文件夾,然后創(chuàng)建index.js

          import Vue from 'vue';import Vuex from 'vuex';

          const modulesFiles = require.context('./modules', true, /\.js$/)const modules = modulesFiles.keys().reduce((modules, modulePath) => { const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') const value = modulesFiles(modulePath) modules[moduleName] = value.default return modules}, {})

          Vue.use(Vuex);export default new Vuex.Store({ modules: modules});

          再在store文件夾下創(chuàng)建modules文件夾,主要用于存放狀態(tài)數(shù)據(jù)模塊文件的,先不用創(chuàng)建文件:

          2a0f6dbe5639b0333ce7f80d1d127604.webp

          然后就是在main.js中裝載vuex,

          import Vue from 'vue'import App from './App.vue'//引入elementUIimport ElementUI from 'element-ui';import 'element-ui/lib/theme-chalk/index.css'; //樣式文件一定要引入
          //載入路由import router from './router/index.js';
          //載入vueximport store from './store/index.js'
          Vue.config.productionTip = false
          Vue.use(ElementUI)new Vue({ store, router, render: h => h(App),}).$mount('#app')

          裝載好之后,如果沒報錯的話,那么對于必要的三件套已經是安裝完成了。

          其實還有一個插件是必用的,就是關于網(wǎng)絡請求的,但這篇內容已經很多了,后面用單獨一章來幫助大家了解怎么封裝網(wǎng)絡請求和裝哪個網(wǎng)絡請求的插件。

          好了,這章的內容就先到這了,下一章說一下完善項目的架構。

          永遠不要因為年齡,而去放棄一顆年輕的心。

          --網(wǎng)絡


          瀏覽 59
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲综合大片69999 | 亚洲视频中文 | 瘦精品无码一区二区三区四区五区六区七区八区 | www.色护士 | 午夜1级操逼视频 |