欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決在vue+webpack開發(fā)中出現(xiàn)兩個(gè)或多個(gè)菜單公用一個(gè)組件問題

 更新時(shí)間:2017年11月28日 12:02:04   作者:風(fēng)雨后見彩虹  
這篇文章主要介紹了在vue+webpack實(shí)際開發(fā)中出現(xiàn)兩個(gè)或多個(gè)菜單公用一個(gè)組件的解決方案,需要的朋友可以參考下

在vue的實(shí)際開發(fā)中往往會(huì)遇到公用一個(gè)組件的問題,比如有一個(gè)菜單中的兩個(gè)按鈕,點(diǎn)擊每個(gè)按鈕調(diào)用的是同一個(gè)組件,其內(nèi)容是根據(jù)路由的參數(shù)的不同來請(qǐng)求不同的內(nèi)容。

第一步,首先新建一個(gè)vue+webpack+vuecli的demo,如下操作:

全局安裝vue-cli,vue-cil是vue的腳手架工具,安裝命令:

npm install -g vue-cli

第二步,進(jìn)入到工程目錄中,創(chuàng)建一個(gè)vuedemo的文件夾工程,如下兩步操作:

cd vue_test_project //進(jìn)入vue_test_project目錄下
vue init webpack vuedemo //在vue_test_project目錄下創(chuàng)建一個(gè)vuedemo工程

輸入這個(gè)命令之后,會(huì)出現(xiàn)一些提示,是什么不用管,一直按回車即可。

第三步,如下操作:

cd vuedemo
npm install

執(zhí)行npm install需要一點(diǎn)時(shí)間,因?yàn)闀?huì)從服務(wù)器上下載代碼啦之類的。并且在執(zhí)行過程中會(huì)有一些警告信息。不用管,等著就是了。如果長時(shí)間沒有響應(yīng),就ctrl+c停止掉,然后再執(zhí)行一次即可。

最后一步,操作如下:

npm run dev

在運(yùn)行了npm run dev之后,會(huì)自動(dòng)打開一個(gè)瀏覽器窗口,就可以看到實(shí)際的效果了。這個(gè)demo就創(chuàng)建好了?,F(xiàn)在就在這個(gè)demo中添加一些內(nèi)容,修改成如下:

修改HelloWorld.vue的內(nèi)容為如下:

<template>
 <div class="hello">
 <h1>{{ msg }}</h1>
 <h2>Essential Links</h2>
 <div class="btn">
  <router-link :to="{name:'content',params:{differId:'con1'}}">內(nèi)容按鈕1</router-link>
  <router-link :to="{name:'content',params:{differId:'con2'}}">內(nèi)容按鈕2</router-link>
 </div>
 <router-view></router-view>
 </div>
</template>
<script>
export default {
 name: 'HelloWorld',
 data () {
 return {
 msg: 'Welcome to Your Vue.js App'
 }
 }
}
</script>
<style scoped>
h1, h2 {
 font-weight: normal;
}
ul {
 list-style-type: none;
 padding: 0;
}
li {
 display: inline-block;
 margin: 0 10px;
}
a {
 color: #42b983;
}
</style>

路由router下的index.html的修改為如下:

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import content from '@/components/conDetail'
Vue.use(Router)
export default new Router({
 routes: [
 {
 path: '/',
 name: 'HelloWorld',
 component: HelloWorld,
 children:[
  {name:'content',path:'content/:differId',component:content}
 ]
 }
 ]
})

現(xiàn)在創(chuàng)建一個(gè)conDetail.vue了,如下:

<template>
 <div class="same">
 這個(gè)是相同的內(nèi)容
 <div class="conlist">
  <template v-for="item in items">
  <p>{{item.con}}</p>
  </template>
 </div>
 </div>
</template>
<script>
export default {
 name: 'conDetail',
 data () {
 return {
 msg: '',
 differIdType:'',
 conlist:[
  {'con':'這是第一個(gè)內(nèi)容按鈕的內(nèi)容1'},
  {'con':'這是第一個(gè)內(nèi)容按鈕的內(nèi)容2'}
 ],
 items:[], 
 }
 },
 mounted(){
  this.differIdType = this.$route.params.differId == 'con1' ? '0' : '1';
  if(this.differIdType == 0){
  this.items = this.conlist;
  }else{
  this.items = [];
  }
 },
 watch:{
 $route:function(to,from){
  this.differIdType = to.params.differId == 'con1' ? '0' : '1'; 
  if(this.differIdType == 0){
  this.items = this.conlist;
  }else{
  this.items = [];
  } 
 }
 }
}
</script>
<style>
</style>

結(jié)果就是,當(dāng)點(diǎn)擊內(nèi)容按鈕1,出現(xiàn)了對(duì)象的內(nèi)容,點(diǎn)擊內(nèi)容按鈕2,出現(xiàn)相應(yīng)的內(nèi)容。當(dāng)然我這兒寫的是點(diǎn)擊按鈕2的時(shí)候,其items的內(nèi)容為空數(shù)組。這兒也使用了$route的監(jiān)聽。

復(fù)用組件時(shí),想對(duì)路由參數(shù)的變化作出響應(yīng)的話,你可以簡(jiǎn)單地 watch(監(jiān)測(cè)變化) $route 對(duì)象:

const User = {
 template: '...',
 watch: {
 '$route' (to, from) {
  // 對(duì)路由變化作出響應(yīng)...
 }
 }
}

或者使用 2.2 中引入的 beforeRouteUpdate 守衛(wèi):

const User = {
 template: '...',
 beforeRouteUpdate (to, from, next) {
 // react to route changes...
 // don't forget to call next()
 }
}

總結(jié)

以上所述是小編給大家介紹的解決在vue+webpack開發(fā)中出現(xiàn)兩個(gè)或多個(gè)菜單公用一個(gè)組件問題,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論