Vue中配置路由vue-router

1、構(gòu)建路由頁面組件

在src/components/下 創(chuàng)建 項目所需的頁面 暫時不寫什么內(nèi)容
比如 home.vue

<template>
  <div class="home">
    <h1>{{ msg }}</h1>
  </div>
</template>
<script>
export default {
  name: 'home',
  data () {
    return {
      msg: 'Home'
    }
  }
}
</script>

2、建立Vue-Router路由配置

在src/router/index.js 中

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/Home'
import NotFound from '@/views/NotFound'
Vue.use(VueRouter)
const routes = [{
  path: '/home',
  name: 'Home',
  component: Home
},
{
  path: '/',
  redirect: '/home'
},
{
  path: '*',
  component: NotFound
}]
export default routes

在src/main.js 中

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import App from './App'
import VueRouter from 'vue-router'
import routes from './router'
Vue.use(VueRouter)

const router = new VueRouter({
  routes
})

FastClick.attach(document.body)

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  router,
  render: h => h(App)
}).$mount('#app-box')

3、配置入口(默認生成,已配置好)

根目錄 app.vue

<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'app'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
npm run dev

4、遇到的問題

錯誤信息:

 ?  http://eslint.org/docs/rules/no-mixed-spaces-and-tabs  
Mixed spaces and tabs                             
  /Users/derek/Documents/myProject/vue/firstVue/src/App.vue:13:2
          message: 'Hello Vue!'
       ^
  ?  http://eslint.org/docs/rules/no-tabs                   
Unexpected tab character                          
  /Users/derek/Documents/myProject/vue/firstVue/src/App.vue:14:2
        }

錯誤原因:因為你設(shè)置了eslint,安裝時Use ESLint to lint your code? (Y/n)
如果你想有良好的規(guī)范,其實錯誤已經(jīng)很清晰,大多數(shù)就是縮進不規(guī)范,分號不需要等原因,很容易解決的。寫多了就成習慣了。

解決方案:找到build/webpack.base.conf.js

刪掉下面代碼:

{
    test: /\.(js|vue)$/,
    loader: 'eslint-loader',
    enforce: 'pre',
    include: [resolve('src'), resolve('test')],
    options: {
      formatter: require('eslint-friendly-formatter')
    }
  },
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文基于工作項目開發(fā),做的整理筆記因工作需要,項目框架由最初的Java/jsp模式,逐漸轉(zhuǎn)移成node/expre...
    SeasonDe閱讀 7,541評論 3 35
  • 感賞兒子今天情緒平穩(wěn)了很多,玩了一天的游戲,很累的睡去。聽見他爸爸說去野外,很感興趣,終于有了一件健康有意...
    金色陽光魏艷春閱讀 330評論 0 0
  • 一屆屆學(xué)生教下來,不但沒有對自己教學(xué)的總結(jié),反倒更添了許多疑惑,眼前學(xué)生變化之大完全超出預(yù)想,愛學(xué)習、善吃苦的孩子...
    瘋讀閱讀 561評論 0 1
  • 華夏五千年文明起始于炎黃,歷史的細節(jié)雖多不可考,但民族的根源始終堅固。 史記自五帝本紀始,講述一段模糊而遙遠的歷史...
    獼猴桃姑娘閱讀 338評論 0 0
  • 親親相隱的正義感 今天,我不想直接下結(jié)論。 先來說一下“父攘子證”的這個人,聽聞這件事兒之后你會有什么想法?這個人...
    貓小埋閱讀 213評論 0 0

友情鏈接更多精彩內(nèi)容