vue-cli 路由webpack打包過程
安裝cnpm cnpm npm install -g cnpm
安裝腳手架 cnpm install -g vue-cli
安裝webpack-simple模板 并創(chuàng)建一個(gè)demo01的文件夾vue init webapck-simple demo01
進(jìn)入demo01文件cd demo01
下載webpack-simple的依賴 cnpm init
開啟虛擬服務(wù)器 npm run dev
下載路由 cnpm install vue-router -S
打開dome01里的main.js
import Vue from 'vue'
import vueRouter from "vue-router"
//引進(jìn)路由
import App from './App.vue'
Vue.use(vueRouter)
//使用路由
new Vue({
el: '#app',
render: h => h(App)
})
在scr里新建components文件。在components文件里新建兩個(gè)組件文件Mains.vue和Deail.vue
在App.vue里寫:
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'app',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: left;
color: #2c3e50;
margin-top: 60px;
}
</style>
Detail.vue 里寫:
<template>
<div>
<h1>{{arr}}</h1>
<h2>索引是{{this.$route.params.xiabiao}}</h2>
</div>
</template>
<script>
export default{
//暴露一下
data:function(){
return{
arr:""
}
},
beforeMount:function(){
this.arr=this.$route.params.xiabiao;
}
}
</script>
Mains.vue里寫:
<template>
<div>
<h1>
this is my list
</h1>
<input type="text" v-model="TypeValue">
<button @click="adds()" :disabled="flg">add</button>
<p v-show="flg">{{no}}</p>
<ul>
<li v-for="(value,index) in num"><router-link :to="'/detail/'+index">{{value}}</router-link></li>
<!--<li v-for="(value,index) in num"><router-link to="/datai/pp[p/p;pl">{{value}}</router-link></li>-->
</ul>
</div>
</template>
<script>
export default{
data:function(){
return{
num:['10','20','30'],
TypeValue:"",
no:"有重復(fù)的數(shù)據(jù)",
flg:false
}
},
watch:{
TypeValue:function(){
if(this.num.indexOf(this.TypeValue)==-1){
this.flg=false
}else {
this.flg=true
}
}
},
methods:{
adds:function(){
this.num.push(this.TypeValue);
this.TypeValue='';
}
}
}
</script>
main.js里改為:
import Vue from 'vue'
import vueRouter from "vue-router"
//引進(jìn)路由
import App from './App.vue'
import routerConfig from './router.config.js'
Vue.use(vueRouter)
//使用路由
const myRouter=new vueRouter(routerConfig)
//新建路由實(shí)例
new Vue({
el: '#app',
render: h => h(App),
router:myRouter
//掛載到vue 上
})
src里寫一個(gè) router.config.js 配置路由:
import Mains from "./components/Mains.vue"
import Detail from "./components/Detail.vue"
//import 引入 自定名字 路徑是‘href’
export default{
// 暴露文件
//出口文件
routes:[
{path:'/mains',component:Mains},
{path:'/detail/:xiabiao',component:Detail},
{path:'/',component:Mains}
]
}
最后編輯于 :2017.11.20 12:17:49
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者 【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。 平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。