2017-09-06 4 views
1
<script> 
import alertStore from '../stores/alert'; 
Vue.component('alert', require('vue-strap').alert); 

export default { 
    data() { 
     return { 
      show: alertStore.state.show, 
      title: alertStore.state.title, 
      msg: alertStore.state.msg, 
      type: alertStore.state.type 
     } 
    }, 
    created() { 
    }, 
    computed: { 
     title() { 

      return alertStore.state.title; 
     }, 
     msg() { 
      return alertStore.state.msg; 
     }, 
     type() { 
      return alertStore.state.type; 
     }, 
     show() { 
      return alertStore.state.show; 
     }, 
     duration() { 
      return alertStore.state.duration; 
     } 
    }, 
    methods: { 
     dismissAlert: function() { 
      this.$store.dispatch('dismissAlert', {title: '...'}); 
     }, 
    } 
} 

vueJS [Vueが警告]を:計算されたプロパティ "タイトル" はすでにデータに定義されている

どのようにしてVueの中に名前空間の仕事?データキー、計算された戻りオブジェクトキー、およびすべてのコンポーネントオブジェクトキーの両方がこのインスタンスに追加されますか?

これを上書きすると

[Vue警告]:計算されたプロパティ "title"はすでにデータに定義されています。
[Vue警告]:計算されたプロパティ "show"は既にデータに定義されています。
[Vue警告]:計算されたプロパティ "type"は既にデータに定義されています。
[Vue警告]:計算されたプロパティ "msg"は既にデータに定義されています。

どうすれば解決できますか?

ありがとうございます。

+0

本当に計算されたプロパティを使用する必要がありますか?なぜ、コンポーネントのメンバーを「データ」と「計算済み」に定義していますか? – chrigu

+0

実際にこのコードは別の開発者によって作成されたもので、私はvuejsで新しくなっています。私はvueJSのチュートリアルを読んでいますが、データオブジェクトにデータを追加する理由は何も見つかりません。私がデータオブジェクトからの戻り値を取り除いても、エラーは出ず、テンプレートは表示されません。 Vuejsデベロッパーツールを表示する計算された読み込み:false 何ができるかわかりません。あなたのテンプレートの –

+0

ポストコード – Pradeepb

答えて

0
<style lang="sass" scoped> 
    @import '../../sass/_variables.scss'; 
    .dismiss { 
     cursor: pointer; 
     position: absolute; 
     right: 0; 
     top: 0; 
     padding: 10px; 
     margin: 10px; 
    } 
    .alert { 
     width: 40%; 
     border-radius: 0; 
     border-width: 5px; 
     margin: 10px; 
     .row { 
      margin: 0; 
      .header { 
       font-size: 1.25em; 
       font-weight: 800; 
      } 
     } 
    } 
</style> 
<template> 
    <div> 
     <alert class='card' v-show="show" placement="top" :duration="duration" :type="type"> 
      <div class="row"> 
       <div class="header"> 
        {{ title }} 
       </div> 
       <div class='message'> 
        {{ msg }} 
       </div> 
      </div> 
      <div class="dismiss" title="Click to dismiss" @click="dismissAlert"> 
       <i class="fa fa-times" aria-hidden="true"></i> 
      </div> 
     </alert> 
    </div> 
</template> 
<script> 
    import alertStore from '../stores/alert'; 
    Vue.component('alert', require('vue-strap').alert); 

    export default { 
     data() { 
      return { 
       show: alertStore.state.show, 
       title: alertStore.state.title, 
       msg: alertStore.state.msg, 
       type: alertStore.state.type 
      } 
     }, 
     created() { 
     }, 
     computed: { 
      title() { 
       return alertStore.state.title; 
      }, 
      msg() { 
       return alertStore.state.msg; 
      }, 
      type() { 
       return alertStore.state.type; 
      }, 
      show() { 
       return alertStore.state.show; 
      }, 
      duration() { 
       return alertStore.state.duration; 
      } 
     }, 
     methods: { 
      dismissAlert: function() { 
       this.$store.dispatch('dismissAlert', {title: '...'}); 
      }, 
     } 
    } 
</script> 

これは私のテンプレートコード

0

Vueのは、インスタンスのルートにデータメソッド内のすべてのプロパティをバインドしています。また、これは計算されたプロパティとメソッドでも行われるため、名前の競合を避けるために別の名前を使用する必要があります。

投稿したコードの主な問題は、すべてのデータプロパティに名前の競合があることです。実際には、Vuexストアを使用しているため、計算されたプロパティだけではデータプロパティはまったく必要ありません。

これは、Vuexストアを使用する最善の方法でもありません。一般的な推奨事項はmapGettersを文書hereとして使用することです。

関連する問題