[精讚] [會員登入]
1924

vue.js components 多個組件的呈現

vue.js 組件 component

分享此文連結 //n.sfs.tw/10292

分享連結 vue.js components 多個組件的呈現@igogo
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-22 15:15:30 最後編修
2016-11-25 17:32:23 By igogo
 

這是一個利用vue.js 將json data 取回來   並依點擊的事件 讓不同component 顯示不同的內容的範例

 

data.json

  {
    "_id": "58379018146a09705a1dd0eb",
    "guid": "aa2361a6-c49c-46a0-837d-5bfff6924b89",
    "isActive": true,
    "age": 22,
    "car": "gogoro",
    "pet": "cat"
  },
  {
    "_id": "583790181eaefda6f6f74c93",
    "guid": "e694dafa-5f8d-4d95-8d9f-ef5d7f4df589",
    "isActive": false,
    "age": 24,
    "car": "toyota",
    "pet": "doggy"
  },
  {
    "_id": "58379018447bc827cdd99557",
    "guid": "7dbb0102-e8e8-4ec6-8c93-eb747d33eff3",
    "isActive": true,
    "age": 39,
    "car": "kymco",
    "pet": "pig"
  }
]

 

註冊組件 

 Vue.component('car', {
            props: ['message', 'items'],
            template: '<div>{{message}} cars, which are <ul><li v-for="item in items">{{item.car}}</li></ul></div>',
        });

        Vue.component('pet', {
            props: ['message', 'items'],
            template: '<div>{{message}} pets, which are <ul><li v-for="item in items">{{item.pet}}</li></ul></div>',
        });

 

props 裡的message 及items 是由父組件來的

 

父組件

var app = new Vue({
            el: '#app',
            data: {
                currentView: '',
                message: 'My frends have ',
                items: []
            },
            methods: {
                getData: function(currentView) {
                    this.currentView = currentView;
                    this.$http.get('data.json', {
                            params: {
                                ID: 12345
                            }
                        })
                        .then(function(response) {
                            this.items = response.data;

                            console.log(this.items);
                        })
                        .catch(function(error) {
                            console.log(error);
                        });

                }
            }
        });

 

預設的view 是父組件,當使用者按下按鈕後,會因觸發不同的事件產生不同的結果,currentView的值會因此對應到

 <component v-bind:is="currentView" :message="message" :items="items">
            <!-- component changes when vm.currentView changes! -->
  </component>

 

:items="items"  是 v-bind:items="items" 的縮寫  讓字面更為簡潔

 

完整寫法

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
    <!-- Latest compiled and minified JavaScript -->
    < script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
    integrity = "sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
    crossorigin = "anonymous" >
    </script>
    <script src="//unpkg.com/vue/dist/vue.js"></script>
    <script src="//unpkg.com/vue-resource@1.0.3/dist/vue-resource.min.js"></script>
</head>

<body>
    <div id="app">
        <component v-bind:is="currentView" :message="message" :items="items">
            <!-- component changes when vm.currentView changes! -->
        </component>
        <button @click="getData('car')" class="btn">car</button>
        <button @click="getData('pet')" class="btn">pet</button>
    </div>
    <footer>
      
        <script type="text/javascript">
        Vue.component('car', {
            props: ['message', 'items'],
            template: '<div>{{message}} cars, which are <ul><li v-for="item in items">{{item.car}}</li></ul></div>',
        });

        Vue.component('pet', {
            props: ['message', 'items'],
            template: '<div>{{message}} pets, which are <ul><li v-for="item in items">{{item.pet}}</li></ul></div>',
        });

        var app = new Vue({
            el: '#app',
            data: {
                currentView: '',
                message: 'My frends have ',
                items: []
            },
            methods: {
                getData: function(currentView) {
                    this.currentView = currentView;
                    this.$http.get('data.json', {
                            params: {
                                ID: 12345
                            }
                        })
                        .then(function(response) {
                            this.items = response.data;

                            console.log(this.items);
                        })
                        .catch(function(error) {
                            console.log(error);
                        });

                }
            }
        });
        </script>
    </footer>
</body>

</html>

 

END

你可能感興趣的文章

[javascript] 將角色物件放到清單中,並依序讀出每個角色的X值 參考在scratch中建立三個角色並且給定值 http://n.sfs.tw/content/index/14716 一

vue js 與teleport vue js 與 teleport 範例 <!DOCTYPE html> <html> <

[vue.js] 動態的props 做parent-child components 雙向綁定 vue.js props components camel-case

將google試算表當作簡易資料庫,利用Google apps cript 在網頁上操作查詢 將google試算表當作簡易資料庫,利用apps cript 在網頁上操作查詢 若我有一試算表資料 縣市 status

vue.js component 在parent與child 傳值 component 在parent與child 傳值

vue.js components 多個組件的呈現 vue.js 組件 component

隨機好文

臺中市雲端校務系統與Windows AD帳號整合(3) LDAP的基本概念

java lambda files filter java, files filter, lambda

google sheet 限制使用者以點選的方式填答 google sheet 限制使用者以點選的方式填答

[web]校園食材登錄平臺午餐食材嵌入頁面語法 校園食材登錄平臺午餐食材嵌入頁面語法

[shell script] 批次判斷domain name 正解設定 判斷dns 正解設定