2017-08-30 8 views
1

私はVueを使い始めていて、いくつかの問題を抱えています。最初に、私はこのチュートリアルに従っていました:eventbus。すべてのコード(html、JS、CSS)を1つのhtmlファイルに入れると、このチュートリアルで説明したように動作します。VUEを使用している不明なカスタム要素

しかし私は読んでいて、私はVUE cliアプリの構造に従っていました。私は vue init webpack vueapp01 を使用しました。したがって、私はappフォルダにvueapp01ルートフォルダのindex.htmlファイルを持っています。私はapp.vueと2つのvueファイルをコンポーネントフォルダに持っています。 the-box.vueおよびthe-button.vue; vueテンプレートwebpackによって読み込まれた他のすべてのファイルとともに表示されます。

代わりに(作品)1つのHTMLファイル内のすべてのコードを有していると私は、コードは次のように分離してい: のindex.html:

<!DOCTYPE html> 
 
<html> 
 
    
 
<head> 
 
    <meta charset="utf-8"> 
 
    <title>vueapp01</title> 
 
    <script src="https://unpkg.com/vue/dist/vue.js"></script> 
 
    </head> 
 
    <body> 
 
    <div id="app"></div> 
 
    <!-- built files will be auto injected --> 
 
    </body> 
 
</html>
App.vue:

<template> 
 
<div id="the-example" class="container"> 
 
    <h1>Building an Event Bus with <a href="https://vuejs.org" target="_blank">Vue.js</a></h1> 
 
    <div class="row"> 
 
    <div class="col-xs-6"> 
 
     <the-button what="Event #1"></the-button> 
 
     <the-button what="Event #2"></the-button> 
 
     <the-button what="Event #3"></the-button> 
 
    </div> 
 
    <div class="col-xs-6"> 
 
     <the-box name="Receiver #1"></the-box> 
 
    </div> 
 
    </div> 
 
</div> 
 
    </div> 
 
</template> 
 

 
<script> 
 
    import the-button from './components/the-button' 
 
    import the-box from './components/the-box' 
 

 
    export default { 
 
     name: 'app', 
 
     components: { 
 
     the-button,the-box 
 
     } 
 
    } 
 
</script> 
 
<-- 
 
<script> 
 
/****************************************** 
 
The Central Event Bus Instance 
 
*******************************************/ 
 
let EventBus = new Vue(); 
 

 
</script>
-box.vue:

/****************************************** 
 
Example Root Vue Instance 
 
*******************************************/ 
 
new Vue({el: "#the-example"}); 
 

 
/****************************************** 
 
A sample Vue.js component that emits an event 
 
*******************************************/ 
 

 
let TheButton = Vue.extend({ 
 
\t name: "the-button", 
 
    props: ["what"], 
 
    template: ` 
 
    \t <button class="btn btn-md btn-success the-button" @click="makeItHappen()">Sender: {{what}}</button> 
 
    `, 
 
    methods: { 
 
    \t makeItHappen: function(){ 
 
    \t EventBus.$emit("somethingHappened", this.what) 
 
    } 
 
    } 
 
}); 
 

 
Vue.component("the-button", TheButton);
-button.vue:現在

/****************************************** 
 
A sample Vue.js component that received an event 
 
*******************************************/ 
 

 
let TheBox = Vue.extend({ 
 
\t name: "the-box", 
 
    props: ["name"], 
 
    template: ` 
 
    \t <div class="well"> 
 
    \t <div class="text-muted">{{name}}</div> \t 
 
    \t <div>{{respondedText}}</div> 
 
    </div> 
 
    `, 
 
    data: function(){ 
 
    \t return { 
 
    \t respondedText: null 
 
    } 
 
    }, 
 
\t created: function(){ 
 
    \t EventBus.$on('somethingHappened', (what)=>{ 
 
    \t this.respondedText = 'Event Received: ' + what; 
 
    }) 
 
    \t console.log("Responder") 
 
    } 
 

 
}); 
 

 
Vue.component("the-box", TheBox);

、私は、 "未知のカスタム要素-ボックス"、エラーを取得しています "未知のカスタム要素ボタン" 。私はスクリプトとテンプレートの注文を最初に読み込むように変更しようとしましたが、まだ運がありません。

ご協力いただければ幸いです。また、私はこれらのコンポーネントを別々のファイルに分けて正しく実行していると仮定していますが、正しくない場合はVueを使用する方法を批判しています。

+2

「から 'インポートボタン./components/the-button ''は有効なjavascriptではありません。私はあなたがあなたが言うところまでどのようになっているのかは分かりません。変数にダッシュを付けることはできません。 – Bert

+0

@Bertダッシュを含まないようにコード全体で変更しましたが、私はまだ同じエラーが発生しています。 –

+0

1つのファイルコンポーネントをインポートするときには、通常、 'vue-loader'を通して実行されるように拡張子を指定する必要があります。あなたは '。/ components/the-button.vue''から 'Import TheButton'を使用していますか?拡張に注意してください。 – Bert

答えて

0

ここでは、vue init webpack <project>を使用していると仮定して、1つのファイルコンポーネントでそのフィディルドを実装するためのファイルの完全な例を示します。

index.htmlを

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <title>bus</title> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    </head> 
    <body> 
    <div id="app"></div> 
    <!-- built files will be auto injected --> 
    </body> 
</html> 

main.js

import Vue from 'vue' 
import App from './App' 

Vue.config.productionTip = false 

window.EventBus = new Vue() 

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

のApp。VUE

<template> 
<div id="the-example" class="container"> 
    <h1>Building an Event Bus with <a href="https://vuejs.org" target="_blank">Vue.js</a></h1> 
    <div class="row"> 
    <div class="col-xs-6"> 
     <the-button what="Event #1"></the-button> 
     <the-button what="Event #2"></the-button> 
     <the-button what="Event #3"></the-button> 
    </div> 
    <div class="col-xs-6"> 
     <the-box name="Receiver #1"></the-box> 
     <the-box name="Receiver #2"></the-box> 
     <the-box name="Receiver #3"></the-box> 

    </div> 
    </div> 
</div> 
</template> 

<script> 
    import TheButton from './components/TheButton.vue' 
    import TheBox from './components/TheBox.vue' 

    export default { 
     name: 'app', 
     components: { 
     TheButton, TheBox 
     } 
    } 
</script> 

コンポーネント/ TheBox.vue

<template> 
    <div class="well"> 
     <div class="text-muted">{{name}}</div> 
     <div>{{respondedText}}</div> 
    </div> 
</template> 

<script> 
export default { 
    name: "the-box", 
    props: ["name"], 
    data: function(){ 
    return { 
     respondedText: null 
    } 
    }, 
    created: function(){ 
    EventBus.$on('somethingHappened', (what)=>{ 
     this.respondedText = 'Event Received: ' + what; 
    }) 
    console.log("Responder") 
    } 

} 
</script> 

コンポーネント/ TheButton.vue

<template> 
    <button class="btn btn-md btn-success the-button" @click="makeItHappen()">Sender: {{what}}</button> 
</template> 

<script> 
export default { 
    name: "the-button", 
    props: ["what"], 
    methods: { 
    makeItHappen: function(){ 
     EventBus.$emit("somethingHappened", this.what) 
    } 
    } 

} 
</script> 
+0

それはそれでした!すべてのあなたの助けをありがとう、本当にありがとう! –

2

変更:

import TheButton from './components/the-button' 
import TheBox from './components/the-box' 

import the-button from './components/the-button' 
import the-box from './components/the-box' 

components: { 
    TheButton, 
    TheBox, 
} 

を行うどこかにあなたが何らかの形で見ていない別のはるかに大きいエラーが存在する必要があります。

+0

私はこれらの変更を行いましたが、それでも同じエラーが発生します。 vue.component( "the-box、TheBox")とVue.component( "the-button、TheButton")はvueファイルの一番下にありますか?それらはapp.vueにあるべきですか? –

+0

これより大きなエラーは見つかりませんでした。私は基本的なvue init webpack vue003がインストールされていて、そのアプリケーションはうまく動作するので、VUEがうまく機能していることを知っています。 –

関連する問題