解決在vue+webpack開發(fā)中出現(xiàn)兩個或多個菜單公用一個組件問題
在vue的實際開發(fā)中往往會遇到公用一個組件的問題,比如有一個菜單中的兩個按鈕,點擊每個按鈕調(diào)用的是同一個組件,其內(nèi)容是根據(jù)路由的參數(shù)的不同來請求不同的內(nèi)容。
第一步,首先新建一個vue+webpack+vuecli的demo,如下操作:
全局安裝vue-cli,vue-cil是vue的腳手架工具,安裝命令:
npm install -g vue-cli
第二步,進入到工程目錄中,創(chuàng)建一個vuedemo的文件夾工程,如下兩步操作:
cd vue_test_project //進入vue_test_project目錄下 vue init webpack vuedemo //在vue_test_project目錄下創(chuàng)建一個vuedemo工程
輸入這個命令之后,會出現(xiàn)一些提示,是什么不用管,一直按回車即可。
第三步,如下操作:
cd vuedemo npm install
執(zhí)行npm install需要一點時間,因為會從服務器上下載代碼啦之類的。并且在執(zhí)行過程中會有一些警告信息。不用管,等著就是了。如果長時間沒有響應,就ctrl+c停止掉,然后再執(zhí)行一次即可。
最后一步,操作如下:
npm run dev
在運行了npm run dev之后,會自動打開一個瀏覽器窗口,就可以看到實際的效果了。這個demo就創(chuàng)建好了?,F(xiàn)在就在這個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)建一個conDetail.vue了,如下:
<template>
<div class="same">
這個是相同的內(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':'這是第一個內(nèi)容按鈕的內(nèi)容1'},
{'con':'這是第一個內(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é)果就是,當點擊內(nèi)容按鈕1,出現(xiàn)了對象的內(nèi)容,點擊內(nèi)容按鈕2,出現(xiàn)相應的內(nèi)容。當然我這兒寫的是點擊按鈕2的時候,其items的內(nèi)容為空數(shù)組。這兒也使用了$route的監(jiān)聽。
復用組件時,想對路由參數(shù)的變化作出響應的話,你可以簡單地 watch(監(jiān)測變化) $route 對象:
const User = {
template: '...',
watch: {
'$route' (to, from) {
// 對路由變化作出響應...
}
}
}
或者使用 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)兩個或多個菜單公用一個組件問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue生成token保存在客戶端localStorage中的方法
本篇文章主要介紹了vue生成token保存在客戶端localStorage中的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
vue頁面不能根據(jù)路徑進行跳轉(zhuǎn)的解決方法
本文主要介紹了vue頁面不能根據(jù)路徑進行跳轉(zhuǎn)的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-12-12
Vue3+Element-Plus使用Table預覽圖片發(fā)生元素遮擋的解決方法
這篇文章主要介紹了Vue3+Element-Plus使用Table預覽圖片發(fā)生元素遮擋的問題分析和解決方法,文中通過代碼示例講解的非常詳細,對大家解決問題有一定的幫助,需要的朋友可以參考下2024-04-04
mpvue小程序循環(huán)動畫開啟暫停的實現(xiàn)方法
這篇文章主要介紹了mpvue小程序循環(huán)動畫開啟暫停的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05

