0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Vue.js】vue-cliでのVueインスタンスの記述について

Last updated at Posted at 2021-03-14

vue-cliでのVueインスタンスの記述について

背景

Vue-cliを使うと、Vueと違ってVueインスタンス部分のdataやmethodsなど、

  • data:data()

のように書き方が少し違う(自分はググって出てきた記事等を参照して書いていた)が、こちらは公式のガイド等はないのだろうか

結論

同じ種類のコンポーネントの異なるインスタンス間で、独立した data を扱うために、data はオブジェクトではなく、オブジェクトを返す関数でなくてはならない

コンポーネントの全てのインスタンスが同じデータオブジェクトを参照しているので、1つのリストのタイトルを変えることは、他の全てのリストのタイトルを変えることになる。

よって、CLIの場合は関数にしていないとやはりだめなよう
また、CDNの場合もオブジェクトを返す関数にした方がよいよう

参考

CDNを使用した際は、data: {} でも data: function() { return {} }のどちらでもエラーは出ない

この記事は上記の公式をもう少し噛み砕いてマス

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?