最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue組件數(shù)據(jù)傳遞、父子組件數(shù)據(jù)獲取,slot,router路由功能示例

 更新時(shí)間:2019年03月19日 09:45:56   作者:白楊-M  
這篇文章主要介紹了vue組件數(shù)據(jù)傳遞、父子組件數(shù)據(jù)獲取,slot,router路由功能,結(jié)合實(shí)例形式分析了vue.js組件數(shù)據(jù)傳遞、路由相關(guān)概念、原理及相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了vue組件數(shù)據(jù)傳遞、父子組件數(shù)據(jù)獲取,slot,router路由功能。分享給大家供大家參考,具體如下:

一、vue默認(rèn)情況下,子組件也沒(méi)法訪問(wèn)父組件數(shù)據(jù)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
  <style>
  </style>
</head>
<body>
  <div id="box">
    <aaa>
    </aaa>
  </div>
  <script>
    var vm=new Vue({
      el:'#box',
      data:{
        a:'aaa'
      },
      components:{
        'aaa':{
          data(){
            return {
              msg:'我是父組件的數(shù)據(jù)'
            }
          },
          template:'<h2>我是aaa組件</h2><bbb></bbb>',
          components:{
            'bbb':{
              template:'<h3>我是bbb組件->{{msg}}</h3>'//這里是子組件,是訪問(wèn)不到父組件的數(shù)據(jù)msg
            }
          }
        }
      }
    });
  </script>
</body>
</html>

二、數(shù)據(jù)傳遞

組件數(shù)據(jù)傳遞:    √

1. 子組件獲取父組件data

在調(diào)用子組件:

<bbb :m="數(shù)據(jù)"></bbb>

子組件之內(nèi):

props:['m','myMsg']
props:{
  'm':String,
  'myMsg':Number
}

2. 父級(jí)獲取子級(jí)數(shù)據(jù)

*子組件把自己的數(shù)據(jù),發(fā)送到父級(jí)
vm.$emit(事件名,數(shù)據(jù));
v-on:    @

1、子組件獲取父組件data

方法一:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
  <style>
  </style>
</head>
<body>
  <div id="box">
    <aaa></aaa>
  </div>
  <template id="aaa">
    <h1>11111</h1>
    <bbb :mmm="msg2" :my-msg="msg"></bbb>
  </template>
  <script>
    var vm=new Vue({
      el:'#box',
      data:{
        a:'aaa'
      },
      components:{
        'aaa':{
          data(){
            return {
              msg:111,
              msg2:'我是父組件的數(shù)據(jù)'
            }
          },
          template:'#aaa',
          components:{
            'bbb':{
              props:['mmm','myMsg'],//my-msg在這里要變成駝峰命名法
              template:'<h3>我是bbb組件->{{mmm}} <br> {{myMsg}}</h3>'
            }
          }
        }
      }
    });
  </script>
</body>
</html>

方法二:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
  <style>
  </style>
</head>
<body>
  <div id="box">
    <aaa></aaa>
  </div>
  <template id="aaa">
    <h1>11111</h1>
    <bbb :mmm="msg2" :my-msg="msg"></bbb>
  </template>
  <script>
    var vm=new Vue({
      el:'#box',
      data:{
        a:'aaa'
      },
      components:{
        'aaa':{
          data(){
            return {
              msg:111,
              msg2:'我是父組件的數(shù)據(jù)'
            }
          },
          template:'#aaa',
          components:{
            'bbb':{
              props:{
                'm':String,//注明數(shù)據(jù)類型
                'myMsg':Number
              },
              template:'<h3>我是bbb組件->{{mmm}} <br> {{myMsg}}</h3>'
            }
          }
        }
      }
    });
  </script>
</body>
</html>

2、 父級(jí)獲取子級(jí)數(shù)據(jù)

方法一:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
  <style>
  </style>
</head>
<body>
  <div id="box">
    <aaa></aaa>
  </div>
  <template id="aaa">
    <span>我是父級(jí) -> {{msg}}</span>
    <bbb @child-msg="get"></bbb>
  </template>
  <template id="bbb">
    <h3>子組件-</h3>
    <input type="button" value="send" @click="send">
  </template>
  <script>
    var vm=new Vue({
      el:'#box',
      data:{
        a:'aaa'
      },
      components:{
        'aaa':{
          data(){
            return {
              msg:'我是父組件的數(shù)據(jù)'
            }
          },
          template:'#aaa',
          methods:{
            get(msg){
              // alert(msg);
              this.msg=msg;
            }
          },
          components:{
            'bbb':{
              data(){
                return {
                  a:'我是子組件的數(shù)據(jù)'
                }
              },
              template:'#bbb',
              methods:{
                send(){
                  this.$emit('child-msg',this.a);
                }
              }
            }
          }
        }
      }
    });
  </script>
</body>
</html>

注意:

  • vm.dispatch(事件名,數(shù)據(jù))子級(jí)向父級(jí)發(fā)送數(shù)據(jù)vm.dispatch(事件名,數(shù)據(jù))子級(jí)向父級(jí)發(fā)送數(shù)據(jù)vm.broadcast(事件名,數(shù)據(jù)) 父級(jí)向子級(jí)廣播數(shù)據(jù)
  • 配合: event:{}
  • 在vue2.0里面已經(jīng),報(bào)廢了

slot:位置、槽口

作用: 占個(gè)位置,不覆蓋原先的內(nèi)容

類似ng里面 transclude (指令)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
  <style>
  </style>
</head>
<body>
  <div id="box">
    <aaa>
      <ul slot="ul-slot">
        <li>1111</li>
        <li>2222</li>
        <li>3333</li>
      </ul>
      <ol slot="ol-slot">
        <li>111</li>
        <li>222</li>
        <li>333</li>
      </ol>
    </aaa>
    <hr>
    <aaa>
    </aaa>
  </div>
  <template id="aaa">
    <h1>xxxx</h1>
    <slot name="ol-slot">這是默認(rèn)的情況</slot>
    <p>welcome vue</p>
    <slot name="ul-slot">這是默認(rèn)的情況2</slot>
  </template>
  <script>
    var vm=new Vue({
      el:'#box',
      data:{
        a:'aaa'
      },
      components:{
        'aaa':{
          template:'#aaa'
        }
      }
    });
  </script>
</body>
</html>

效果圖:

vue-> SPA應(yīng)用,單頁(yè)面應(yīng)用 vue-router路由

    vue-resouce    交互
    vue-router    路由
    路由:根據(jù)不同url地址,出現(xiàn)不同效果
    該課程配套用 0.7.13版本 vue-router
主頁(yè)    home
新聞頁(yè)    news

html:

  <a v-link="{path:'/home'}">主頁(yè)</a>  跳轉(zhuǎn)鏈接
  展示內(nèi)容:
  <router-view></router-view>

js:

  //1. 準(zhǔn)備一個(gè)根組件
  var App=Vue.extend();
  //2. Home News組件都準(zhǔn)備
  var Home=Vue.extend({
    template:'<h3>我是主頁(yè)</h3>'
  });
  var News=Vue.extend({
    template:'<h3>我是新聞</h3>'
  });
  //3. 準(zhǔn)備路由
  var router=new VueRouter();
  //4. 關(guān)聯(lián)
  router.map({
    'home':{
      component:Home
    },
    'news':{
      component:News
    }
  });
  //5. 啟動(dòng)路由
  router.start(App,'#box');

跳轉(zhuǎn):

  router.redirect({
    '/':'/home'
  });

下載vue-router:

vue-router路由:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
  <script src="bower_components/vue-router/dist/vue-router.js"></script>
  <style>
  </style>
</head>
<body>
  <div id="box">
    <ul>
      <li>
        <a v-link="{path:'/home'}">主頁(yè)</a>
      </li>
      <li>
        <a v-link="{path:'/news'}">新聞</a>
      </li>
    </ul>
    <div>
      <router-view></router-view>
    </div>  
  </div>
  <script>
    //1. 準(zhǔn)備一個(gè)根組件
    var App=Vue.extend();
    //2. Home News組件都準(zhǔn)備
    var Home=Vue.extend({
      template:'<h3>我是主頁(yè)</h3>'
    });
    var News=Vue.extend({
      template:'<h3>我是新聞</h3>'
    });
    //3. 準(zhǔn)備路由
    var router=new VueRouter();
    //4. 關(guān)聯(lián)
    router.map({
      'home':{
        component:Home
      },
      'news':{
        component:News
      }
    });
    //5. 啟動(dòng)路由
    router.start(App,'#box');
  </script>
</body>
</html>

跳轉(zhuǎn):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
  <script src="bower_components/vue-router/dist/vue-router.js"></script>
  <style>
  </style>
</head>
<body>
  <div id="box">
    <ul>
      <li>
        <a v-link="{path:'/home'}">主頁(yè)</a>
      </li>
      <li>
        <a v-link="{path:'/news'}">新聞</a>
      </li>
    </ul>
    <div>
      <router-view></router-view>
    </div>  
  </div>
  <script>
    //1. 準(zhǔn)備一個(gè)根組件
    var App=Vue.extend();
    //2. Home News組件都準(zhǔn)備
    var Home=Vue.extend({
      template:'<h3>我是主頁(yè)</h3>'
    });
    var News=Vue.extend({
      template:'<h3>我是新聞</h3>'
    });
    //3. 準(zhǔn)備路由
    var router=new VueRouter();
    //4. 關(guān)聯(lián)
    router.map({
      'home':{
        component:Home
      },
      'news':{
        component:News
      }
    });
    //5. 啟動(dòng)路由
    router.start(App,'#box');
    //6. 跳轉(zhuǎn)
    router.redirect({
      '/':'home' //訪問(wèn)根目錄時(shí),跳轉(zhuǎn)到/home
    });
  </script>
</body>
</html>

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。

希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

新宁县| 潞城市| 尼勒克县| 特克斯县| 陇南市| 海口市| 凉山| 阜南县| 博野县| 当涂县| 大竹县| 庆阳市| 冀州市| 黄浦区| 金坛市| 泰和县| 慈利县| 双城市| 丰城市| 建湖县| 湾仔区| 英超| 丰台区| 临夏市| 南城县| 鞍山市| 深州市| 大英县| 长治县| 田林县| 梁山县| 河津市| 呼图壁县| 资兴市| 离岛区| 桃江县| 航空| 泰安市| 海原县| 罗平县| 涡阳县|