2017-03-07 92 views
6

Sup人!Vue實例內的Vue實例

我這裏得到這個HTML代碼:

// index.html 
<div data-init="component-one"> 
    <...> 

<div data-init="component-two"> 
    <button @click="doSomething($event)"> 
</div> 
</div> 

,如果我理解正確的這一切基本上引用了另一個Vue的實例裏面Vue的實例。相應的JS代碼在兩個文件分割,看起來像這樣:現在

// componentOne.js 
new Vue(
    el: '[data-init="component-one"]', 
    data: {...}, 
    methods: {...} 
); 


// componentTwo.js 
new Vue(
    el: '[data-init="component-two"]' 
    data: {...} 
    methods: { 
    doSomething: function(event) {...} 
    } 
); 

,這個問題是,doSomethingcomponentTwo不會被調用。

但是,當我做一些內聯的東西,如{{ 3 + 3 }},它得到它應該像計算。所以Vue知道有什麼。並且它還會在頁面加載時刪除@click元素。

我也嘗試過與inline-template一起擺弄,但它並不像我預期的那樣在這種情況下工作。而且我認爲它不適合這種情況,所以我再次放棄它。

這裏的正確方法是什麼?我怎樣才能使這項工作成爲現在最簡單的方式?

我們使用的Vue版本是2.1.8

乾杯!

+0

不確定,如果它可以幫助,但你試圖通過「components:[childcomponent]」添加子組件? –

+0

我想知道爲什麼你有2個Vue實例,而不是創建一個主實例和一個組件,它們會更適合你的情況。 如果你創建componentTwo作爲一個真實的組件,那麼你可以從child到root intance發出事件並在那裏捕獲它。 –

+0

也許你應該使用componentOne.vue和componentTwo.vue來代替。閱讀https://vuejs.org/v2/guide/components.html – ytbryan

回答

-1

使用vue-cli創建一個webpack初學者應用程序。 vue init app --webpack

然後,嘗試以這種方式構建組件。瞭解更多:https://vuejs.org/v2/guide/components.html#What-are-Components

  1. 這是main.js

import Vue from 'vue' 
 
import ComponentOne from './ComponentOne.vue' 
 
import ComponentTwo from './ComponentTwo.vue' 
 

 
new Vue({ 
 
    el: '#app', 
 
    template: '<App/>', 
 
    components: { 
 
    ComponentOne, 
 
    ComponentTwo 
 
    } 
 
})

  • 這是ComponentOne.vue
  • <template> 
     
        <div class="user"> 
     
        <div v-for="user in users"> 
     
         <p>Username: {{ user.username }}</p> 
     
        </div> 
     
        </div> 
     
    </template> 
     
    
     
    
     
    <script> 
     
    export default { 
     
        data() { 
     
        return { 
     
         users: [ 
     
         {username: 'Bryan'}, 
     
         {username: 'Gwen'}, 
     
         {username: 'Gabriel'} 
     
         ] 
     
        } 
     
        } 
     
    } 
     
    </script>

  • 這是ComponentTwo.vue
  • <template> 
     
        <div class="two"> 
     
        Hello World 
     
        </div> 
     
    </template> 
     
    
     
    
     
    <script> 
     
    export default { 
     
    
     
    } 
     
    </script>

    +0

    只有當他使用單個文件組件並且使用/已經配置了webpack或使用coresponding加載程序進行瀏覽時,這纔會起作用。 組件註冊'Vue.component('name',{})'有一個全局api,它也可以工作。 –

    +0

    這是真的,我錯過了這個! – ytbryan

    +0

    謝謝你這個片段,它看起來很優雅!我們實際上已經有了Webpack和一切設置。不尋常的代碼組織只是因爲我對先進的Vue東西頗爲陌生。我沒有時間去嘗試和重構今天。我會盡快更新並接受它! –

    4

    但是,當我做一些內聯材料,例如象{{ 3 + 3}},它得到它應該像計算。所以Vue知道有什麼。

    因爲您有父實例'componentOne'。它爲這個模板激活了Vue。如果您需要在其中設置另一個實例,則必須分隔部分模板。例子(它可以滯後於片段!)。 替代

    https://jsfiddle.net/qh8a8ebg/2/ 
    

    // componentOne.js 
     
    new Vue({ 
     
        el: '[data-init="component-one"]', 
     
        data: { 
     
        \t text: 'first' 
     
        }, 
     
        methods: {} 
     
    }); 
     
    
     
    
     
    // componentTwo.js 
     
    new Vue({ 
     
        el: '[data-init="component-two"]', 
     
        data: { 
     
        \t text: 'second' 
     
        }, 
     
        template: `<button @click="doSomething($event)">{{text}}</button>`, 
     
        methods: { 
     
        doSomething: function(event) { 
     
        \t console.log(event); 
     
        } 
     
        } 
     
    });
    <script src="https://vuejs.org/js/vue.min.js"></script> 
     
    <div data-init="component-one"> 
     
    {{text}} 
     
    </div> 
     
    <div data-init="component-two"> 
     
    </div>

    +0

    這正是我過去兩天搜索的解決方案。 謝謝你 – Khaleel

    4

    的問題是,你必須要嵌套兩個彼此VUE實例。 如果元素嵌套,那麼你應該使用相同的實例或嘗試,如果它們是相互獨立的的components

    https://jsfiddle.net/p16y2g16/1/

    // componentTwo.js 
    var item = Vue.component('item',({ 
    name:'item', 
    template:'<button @click="doSomething($event)">{{ message2 }}  </button>', 
    data: function(){ 
        return{ 
        message2: 'ddddddddddd!' 
    }}, 
    methods: { 
    doSomething: function(event) {alert('s')} 
    } 
    })); 
        var app = new Vue({ 
        el: '[data-init="component-one"]', 
        data: { 
    message: 'Hello Vue!' 
        } 
        }); 
    
        <div data-init="component-one"> 
        <button >{{ message }}</button> 
    
        <item></item> 
    
        </div> 
    

    sepatate情況下工作。

    如下:

    https://jsfiddle.net/p16y2g16/

    var app = new Vue({ 
    el: '[data-init="component-one"]', 
    data: { 
        message: 'Hello Vue!' 
        } 
    }); 
    
    
    // componentTwo.js 
    var ddd = new Vue({ 
        el: '[data-init="component-two"]', 
        data: { 
        message: 'ddddddddddd!' 
    }, 
    methods: { 
        doSomething: function(event) {alert('s')} 
    } 
    }); 
    
    +1

    你保存我的日子 – Khaleel

    0
    <div th:if="${msg.replyFloor}"> 
        <div class="msg-lists-item-left"> 
         <span class="msg-left-edit" 
           th:classappend=" ${msg.unreadCount == 0} ? 'msg-all-read' ">您在</span> 
         <span th:text="${msg.topic.title}" 
           class="msg-left-edit-res" 
           th:classappend=" ${msg.unreadCount == 0} ? 'msg-all-read' ">問題回答</span> 
         <span th:text="${msg.type.name}" 
           class="msg-left-edit " 
           th:classappend=" ${msg.unreadCount == 0} ? 'msg-all-read' ">帖子相關</span> 
         <span class="msg-left-edit-number" > 
          產生了<span th:text="${msg.unreadCount} ? : ${msg.unreadCount} + '條新' : ${msg.unreadCount} + '條' " 
             th:class="${msg.unreadCount} ? : 'number-inner':''">2132條</span>回覆 
         </span> 
        </div> 
        <div class="msg-lists-item-right"> 
         <span th:text="${msg.lastShowTime}">2017-8-10</span> 
        </div> 
    </div> 
    
    +0

    給你足夠的解釋你的答案,這將有助於他們更好地理解答案以及將來如何解決這個問題。請參閱[link](https://stackoverflow.com/help/how-to-answer)瞭解如何編寫出色的答案。 –

    0

    內部組件的兩個按鈕元件作爲在Vue的時隙被引用。 對@click指令值的評估發生在父組件(組件-1,哪個主組件-2)中。因此,您需要在那裏聲明點擊處理程序(通過組件1)。

    如果您希望處理程序在component-two內處理,您應該爲其(component-two)模板中的slot元素聲明click指令,並將處理函數作爲pop來傳遞。

    祝你好運。