Skip to content

Migration > Data Option の翻訳を追従 #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/guide/migration/data-option.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ badges:

## 概要

`data` コンポーネントオプション宣言はプレーンな JavaScript `object` を受け入れず、`function` 宣言を期待します。
- **非互換**: `data` コンポーネントオプション宣言はプレーンな JavaScript `object` を受け入れず、`function` 宣言を期待します。

- **非互換**: ミックスインや継承で複数の `data` 返り値をマージする場合、マージはディープではなくシャローになりました(ルートレベルのプロパティのみマージされます)。

## 2.x での構文

Expand Down Expand Up @@ -60,9 +62,60 @@ badges:
</script>
```

## ミックスインをマージする挙動の変更

このほか、コンポーネントとそのミックスインや継承元の `data()` がマージされる際に、マージは*シャロー*で行われるようになりました。

```js
const Mixin = {
data() {
return {
user: {
name: 'Jack',
id: 1
}
}
}
}

const CompA = {
mixins: [Mixin],
data() {
return {
user: {
id: 2
}
}
}
}
```

Vue 2.x での `$data` の結果は:

```json
{
user: {
id: 2,
name: 'Jack'
}
}
```

3.0 では、このような結果に:

```json
{
user: {
id: 2
}
}
```

## 移行の戦略

オブジェクト宣言を利用しているユーザーには以下を推奨します:

- 共有データを外部オブジェクトとして抽出し、それを `data` のプロパティとして使う
- 共有データへの参照、新しい共有オブジェクトを指すようにを書き換える

ミックスインのディープマージに依存しているユーザーには、そのような依存を完全に避けるためにコードをリファクタリングすることをお勧めします。ミックスインのディープマージは非常に暗黙的であり、コードロジックの理解やデバッグがより難しくなる可能性があります。