<address id="ttjl9"></address>

      <noframes id="ttjl9"><address id="ttjl9"><nobr id="ttjl9"></nobr></address>
      <form id="ttjl9"></form>
        <em id="ttjl9"><span id="ttjl9"></span></em>
        <address id="ttjl9"></address>

          <noframes id="ttjl9"><form id="ttjl9"></form>

          Vue 全局變量,局部變量

          2019-4-17    seo達人

          如果您想訂閱本博客內容,每天自動發到您的郵箱中, 請點這里

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

          <body>
          <div id="app">
              <my-component></my-component>
          </div>
          <script src="lib/vue-2.4.0.js"></script>
          <script>
          //全局組件
              Vue.component('myComponent',{
                  //1.組件的內容/模板
                  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提供了一個標簽  component標簽(理解為一個占位符), 用來展示對應名稱的組件  :is屬性設置指定的組件名  -->
              <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 }}
                  子組件的數據
              </h1>
          </template>
          <template id="grandSon">
              <h1>孫子組件的數據</h1>
          </template>
          <!--1.子組件不能訪問父組件的數據
          2. 解決辦法: ①在引用子組件時, 通過屬性綁定 v-bind方法, 把需要傳遞給子組件的數據以綁定的形式傳過來
                        ② 在子組件配置項里添加 props: ['傳遞過來的數據']-->
          <script src="lib/vue-2.4.0.js"></script>
          <script>
              let vm = new Vue({
                  el:"#app",
                  data:{
                      father:'啊~~這是父組件的數據'
                  },
                  methods:{
                  },
                  //局部子組件
                  components:{
                      // 組件名: {配置項}
                      "myComponent":{
                          template:'#box1',
                          data(){
                              return {
                                  msg:"哈哈"
                              }
                          },
                          //在子組件配置項里添加 props: ['傳遞過來的數據']
                          //注意: 組件中所有的props中的數據, 都是通過父組件傳遞給子組件的, props中的數據是只讀, 無法修改
                          props:['fromfather'],
                          methods:{
                              change(){
                                 // this.fromfather = "被修改了"
                              }
                          },
                          //局部子子組件
                          components:{
                              'grandSon':{
                                  template:'#grandSon'
                              }
                          }
                      }
                  }
              })
          </script>
          </body>
          藍藍設計www.syprn.cn )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的UI界面設計、BS界面設計 、 cs界面設計 、 ipad界面設計 、 包裝設計 、 圖標定制 、 用戶體驗 、交互設計、 網站建設 平面設計服務。

          日歷

          鏈接

          個人資料

          藍藍設計的小編 http://www.syprn.cn

          存檔

          亚洲va欧美va天堂v国产综合