From 9aaf29e3a0c774bdaa142de8c31f8f5f54c9f687 Mon Sep 17 00:00:00 2001 From: Guilherme Eiras Date: Mon, 20 Nov 2017 20:26:17 -0200 Subject: [PATCH 1/2] chore: warn properties that starts with $ or _ close #7070 --- src/core/instance/state.js | 9 ++++++++- test/unit/features/instance/properties.spec.js | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 4fa6f15bac8..508b304be3a 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -141,7 +141,14 @@ function initData (vm: Component) { `Use prop default value instead.`, vm ) - } else if (!isReserved(key)) { + } else if (isReserved(key)) { + warn( + `Property "${key}" will not be proxied on the instance because it ` + + `may conflict with Vue's internal properties. You will have to access it as ` + + `"vm.$data.${key}". Avoid defining component properties that start with _ or $.`, + vm + ) + } else { proxy(vm, `_data`, key) } } diff --git a/test/unit/features/instance/properties.spec.js b/test/unit/features/instance/properties.spec.js index 02aca12fcfa..5225b76c537 100644 --- a/test/unit/features/instance/properties.spec.js +++ b/test/unit/features/instance/properties.spec.js @@ -200,4 +200,13 @@ describe('Instance properties', () => { vm.$listeners = {} expect(`$listeners is readonly`).toHaveBeenWarned() }) + + it('warn data properties that start with _ or $', () => { + new Vue({ + data: { $a: 'a', _b: 'b' }, + template: '
' + }).$mount() + expect(`Property "$a" will not be proxied on the instance`).toHaveBeenWarned() + expect(`Property "_b" will not be proxied on the instance`).toHaveBeenWarned() + }) }) From e5950a3eb126cd01475e216c1dd180c5165461d0 Mon Sep 17 00:00:00 2001 From: Guilherme Eiras Date: Mon, 20 Nov 2017 21:20:23 -0200 Subject: [PATCH 2/2] skip warning in production --- src/core/instance/state.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 508b304be3a..fe09c733c49 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -142,12 +142,14 @@ function initData (vm: Component) { vm ) } else if (isReserved(key)) { - warn( - `Property "${key}" will not be proxied on the instance because it ` + - `may conflict with Vue's internal properties. You will have to access it as ` + - `"vm.$data.${key}". Avoid defining component properties that start with _ or $.`, - vm - ) + if (process.env.NODE_ENV !== 'production') { + warn( + `Property "${key}" will not be proxied on the instance because it ` + + `may conflict with Vue's internal properties. You will have to access it as ` + + `"vm.$data.${key}". Avoid defining component properties that start with _ or $.`, + vm + ) + } } else { proxy(vm, `_data`, key) }