<em id="09ttv"></em>
    <sup id="09ttv"><pre id="09ttv"></pre></sup>
    <dd id="09ttv"></dd>

        • Vue 全局變量,局部變量

          2019-4-17    seo達(dá)人

          如果您想訂閱本博客內(nèi)容,每天自動發(fā)到您的郵箱中, 請點這里

          全局組件和局部組件
          * 1.先定義組件   Vue.component('組件名', { 組件模板對象 })
          *   注意: 組件名不要使用原生的標(biāo)簽名, 若組件名定義時用的是駝峰命名法, 則調(diào)用時用中劃線分割后小寫
          *                       例如: 組件-->mtText   使用時-->   <my-text></my-text>
          * 2.配置組件的模板  注意: 組件的模板內(nèi)容有且只有一個根元素
          * 3.在視圖層里調(diào)用 ,用雙標(biāo)簽
          * 4.組件是一個獨立的作用域, 也可以看成一個特殊的vue實例, 可以有data, methods,computed等等
          *   注意: 組件的data是函數(shù), 函數(shù)中需要返回一個對象作為組件的data
          全局組件案例

          <body>
          <div id="app">
              <my-component></my-component>
          </div>
          <script src="lib/vue-2.4.0.js"></script>
          <script>
          //全局組件
              Vue.component('myComponent',{
                  //1.組件的內(nèi)容/模板
                  template: '<div><div>頭部組件</div><h1 @click="fn">呵呵{{msg}}</h1></div>',
                  data(){
                      return {
                          msg:'hello,組件'
                      }
                  },
                  methods:{
                      fn(){
                          console.log(this.msg);
                      }
                  }
              })
              let vm = new Vue({
                  el:"#app",
                  data:{
                  },
                  methods:{

                  },

              })
          </script>
          </body>
          局部組件案例

          <body>
          <div id="app">
              <my-component></my-component>
              <my-test></my-test>
          </div>
          <template id="box1">
              <h1>haha</h1>
          </template>
          <template id="box2">
              <div>
                  <ul>
                      <li v-for="item in arr">
                          {{ item }}
                      </li>
                  </ul>
              </div>
          </template>
          <script src="lib/vue-2.4.0.js"></script>
          <script>
          let vm = new Vue({
                  el:"#app",
                  data:{
                  },
                  methods:{

                  },
                  //局部子組件
                  components:{
                      // 組件名: {配置項}
                      "myComponent":{
                          template:'#box1',
                          data(){
                              return {
                                  msg:"哈哈"
                              }
                          }
                      },
                      "myTest":{
                          template:"#box2",
                          data(){
                              return {
                                  arr:[1,2,3,4]
                              }
                          }
                      }
                  }
              })
          </script>
          </body>
          組件切換:法一

          <body>
          <div id="app">
              <a href="" @click.prevent="flag=true">登錄</a>
              <a href="" @click.prevent="flag=false">注冊</a>
              <login v-if="flag"></login>
              <register v-else="flag"></register>
          </div>
          <script src="lib/vue-2.4.0.js"></script>
          <script>
              Vue.component("login",{
                  template:"<h1>登錄組件</h1>"
              })
              Vue.component("register",{
                  template:"<h1>注冊組件</h1>"
              })
              let vm = new Vue({
                  el:"#app",
                  data:{
                      flag: false
                  },
                  methods:{
                  },
              })
          </script>
          </body>
          組件切換:法二

           <style>
                  .red{
                      color:red;
                  }
                  .v-enter{
                      opacity:0;
                      transform: translateX(150px);
                  }
                  .v-leave-to{
                      opacity:0;
                      transform: translateX(-150px);
                  }
                  .v-enter-active,
                  .v-leave-active{
                      transition: all 0.5s;
                      position: absolute;
                  }
              </style>
          </head>
          <body>
          <div id="app">
              <a href="" :class="{red: flag=='login'}" @click.prevent="flag='login'">登錄</a>
              <a href="" :class="{red: flag=='register'}" @click.prevent="flag='register'">注冊</a>
              <!--  vue提供了一個標(biāo)簽  component標(biāo)簽(理解為一個占位符), 用來展示對應(yīng)名稱的組件  :is屬性設(shè)置指定的組件名  -->
              <transition>
                  <component :is="flag"></component>
              </transition>
          </div>
          <script src="lib/vue-2.4.0.js"></script>
          <script>
              Vue.component("login",{
                  template:"<h1>登錄組件</h1>"
              })
              Vue.component("register",{
                  template:"<h1>注冊組件</h1>"
              })
              let vm = new Vue({
                  el:"#app",
                  data:{
                      flag: "login"
                  },
                  methods:{

                  },
              })
          </script>
          </body>
          父組件向子組件傳值

          <body>
          <div id="app">
              <my-component :fromfather="father"></my-component>
          </div>
          <template id="box1">
              <h1 @click="change">
                  {{ fromfather }}
                  子組件的數(shù)據(jù)
              </h1>
          </template>
          <template id="grandSon">
              <h1>孫子組件的數(shù)據(jù)</h1>
          </template>
          <!--1.子組件不能訪問父組件的數(shù)據(jù)
          2. 解決辦法: ①在引用子組件時, 通過屬性綁定 v-bind方法, 把需要傳遞給子組件的數(shù)據(jù)以綁定的形式傳過來
                        ② 在子組件配置項里添加 props: ['傳遞過來的數(shù)據(jù)']-->
          <script src="lib/vue-2.4.0.js"></script>
          <script>
              let vm = new Vue({
                  el:"#app",
                  data:{
                      father:'啊~~這是父組件的數(shù)據(jù)'
                  },
                  methods:{
                  },
                  //局部子組件
                  components:{
                      // 組件名: {配置項}
                      "myComponent":{
                          template:'#box1',
                          data(){
                              return {
                                  msg:"哈哈"
                              }
                          },
                          //在子組件配置項里添加 props: ['傳遞過來的數(shù)據(jù)']
                          //注意: 組件中所有的props中的數(shù)據(jù), 都是通過父組件傳遞給子組件的, props中的數(shù)據(jù)是只讀, 無法修改
                          props:['fromfather'],
                          methods:{
                              change(){
                                 // this.fromfather = "被修改了"
                              }
                          },
                          //局部子子組件
                          components:{
                              'grandSon':{
                                  template:'#grandSon'
                              }
                          }
                      }
                  }
              })
          </script>
          </body>
          藍(lán)藍(lán)設(shè)計www.sdgs6788.com )是一家專注而深入的界面設(shè)計公司,為期望卓越的國內(nèi)外企業(yè)提供卓越的UI界面設(shè)計、BS界面設(shè)計 、 cs界面設(shè)計 、 ipad界面設(shè)計 、 包裝設(shè)計 、 圖標(biāo)定制 、 用戶體驗 、交互設(shè)計、 網(wǎng)站建設(shè) 平面設(shè)計服務(wù)

          日歷

          鏈接

          個人資料

          藍(lán)藍(lán)設(shè)計的小編 http://www.sdgs6788.com

          存檔

          久久综合鬼色88久久精品综合自在自线噜噜| 久久婷婷国产剧情内射白浆| 久久国产一区二区| 国产亚州精品女人久久久久久 | 久久精品国产乱子伦| 亚洲午夜久久久久久久久久| 久久亚洲国产精品一区二区| 久久强奷乱码老熟女网站| 亚洲午夜久久久影院| A级毛片无码久久精品免费| 国产亚洲精久久久久久无码77777| 国产国产成人精品久久| 婷婷国产天堂久久综合五月| 久久精品国产精品亚洲精品| 亚洲AV无码成人网站久久精品大| 久久精品国产精品亚洲精品| 色综合久久综合中文综合网| 久久久精品国产Sm最大网站| 狠狠色丁香久久综合五月| 亚洲国产视频久久| 九九久久精品国产| 国产成人久久AV免费| 深夜久久AAAAA级毛片免费看| 久久婷婷五月综合国产尤物app| 996久久国产精品线观看| 亚洲欧洲精品成人久久奇米网| 九九99精品久久久久久| 漂亮人妻被黑人久久精品| 97视频久久久| 99久久国产亚洲综合精品| 色综合久久中文字幕综合网| 伊人久久大香线蕉精品| 91精品国产高清久久久久久91| 久久人妻少妇嫩草AV无码专区| 国产精品久久久久久久久软件| 久久精品亚洲福利| 久久人人爽人爽人人爽av| 久久精品国产只有精品66| 久久夜色精品国产亚洲av| 久久久久综合国产欧美一区二区| 久久人妻少妇嫩草AV无码蜜桃|