首頁  >  文章  >  web前端  >  在vue+webpack中公用一個元件問題(詳細教學)

在vue+webpack中公用一個元件問題(詳細教學)

亚连
亚连原創
2018-06-23 17:16:491390瀏覽

這篇文章主要介紹了在vue webpack實際開發中出現兩個或多個選單公用一個元件的解決方案,需要的朋友可以參考下

在vue的實際開發中往往會遇到公用一個元件的問題,例如有一個選單中的兩個按鈕,點擊每個按鈕調用的是同一個元件,其內容是根據路由的參數的不同來請求不同的內容。

第一步,先新建一個vue webpack vuecli的demo,如下操作:

全域安裝vue-cli,vue-cil是vue的鷹架工具,安裝指令:

npm install -g vue-cli

第二步,進入到工程目錄中,建立一個vuedemo的資料夾工程,如下兩步操作:

cd vue_test_project //进入vue_test_project目录下
vue init webpack vuedemo //在vue_test_project目录下创建一个vuedemo工程

輸入這個指令之後,會出現一些提示,是什麼不用管,一直按回車即可。

第三步,如下操作:

cd vuedemo
npm install

執行npm install需要一點時間,因為會從伺服器上下載程式碼啦之類的。並且在執行過程中會有一些警告訊息。不用管,等著就是了。如果長時間沒有回應,就ctrl c停止掉,然後再執行一次即可。

最後一步,操作如下:

npm run dev

在運行了npm run dev之後,會自動開啟一個瀏覽器窗口,就可以看到實際的效果了。這個demo就創建好了。現在就在這個demo中加入一些內容,修改成如下:

修改HelloWorld.vue的內容為如下:

<template>
 <p class="hello">
 <h1>{{ msg }}</h1>
 <h2>Essential Links</h2>
 <p class="btn">
  <router-link :to="{name:&#39;content&#39;,params:{differId:&#39;con1&#39;}}">内容按钮1</router-link>
  <router-link :to="{name:&#39;content&#39;,params:{differId:&#39;con2&#39;}}">内容按钮2</router-link>
 </p>
 <router-view></router-view>
 </p>
</template>
<script>
export default {
 name: &#39;HelloWorld&#39;,
 data () {
 return {
 msg: &#39;Welcome to Your Vue.js App&#39;
 }
 }
}
</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 &#39;vue&#39;
import Router from &#39;vue-router&#39;
import HelloWorld from &#39;@/components/HelloWorld&#39;
import content from &#39;@/components/conDetail&#39;
Vue.use(Router)
export default new Router({
 routes: [
 {
 path: &#39;/&#39;,
 name: &#39;HelloWorld&#39;,
 component: HelloWorld,
 children:[
  {name:&#39;content&#39;,path:&#39;content/:differId&#39;,component:content}
 ]
 }
 ]
})

現在創建一個conDetail.vue了,如下:

<template>
 <p class="same">
 这个是相同的内容
 <p class="conlist">
  <template v-for="item in items">
  <p>{{item.con}}</p>
  </template>
 </p>
 </p>
</template>
<script>
export default {
 name: &#39;conDetail&#39;,
 data () {
 return {
 msg: &#39;&#39;,
 differIdType:&#39;&#39;,
 conlist:[
  {&#39;con&#39;:&#39;这是第一个内容按钮的内容1&#39;},
  {&#39;con&#39;:&#39;这是第一个内容按钮的内容2&#39;}
 ],
 items:[], 
 }
 },
 mounted(){
  this.differIdType = this.$route.params.differId == &#39;con1&#39; ? &#39;0&#39; : &#39;1&#39;;
  if(this.differIdType == 0){
  this.items = this.conlist;
  }else{
  this.items = [];
  }
 },
 watch:{
 $route:function(to,from){
  this.differIdType = to.params.differId == &#39;con1&#39; ? &#39;0&#39; : &#39;1&#39;; 
  if(this.differIdType == 0){
  this.items = this.conlist;
  }else{
  this.items = [];
  } 
 }
 }
}
</script>
<style>
</style>

結果就是,當點擊內容按鈕1,出現了物件的內容,點擊內容按鈕2 ,出現對應的內容。當然我這兒寫的是點擊按鈕2的時候,其items的內容為空數組。這兒也使用了$route的監聽。

復用元件時,想對路由參數的變化作出回應的話,你可以簡單地watch(監控變化) $route 物件:

const User = {
 template: &#39;...&#39;,
 watch: {
 &#39;$route&#39; (to, from) {
  // 对路由变化作出响应...
 }
 }
}

或使用2.2 中引入的beforeRouteUpdate 守衛:

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

上面是我整理給大家的,希望未來會對大家有幫助。

相關文章:

使用Angular如何實作基本購物車功能

在node.js中有關路由,中間件的詳細介紹說明

在Vue中如何實作進入/離開動畫

在webpack中詳細解讀入口函數run

以上是在vue+webpack中公用一個元件問題(詳細教學)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn