聚合1,子组件中myObject的更改不会反映在父组件

时间:2018-05-31 17:21:26

标签: polymer polymer-1.0

说明

hola-mundo是一个父组件, hola-mundo2是一个子组件, 当我们按下按钮" changeValueMyObject"时,MyObject由两个组件之间的两个数据绑定共享。价值对象" MyObject"在子组件中更改。但是在父组件中,对象"我的对象"没有改变。

重现错误的步骤:

1.按 - 按钮" changeValueMyObject" (这会更改子组件中的值)

2.按 - 按钮" showValueMyObject" (这显示了父组件中的MyObject值)

预期的行为:

{id:1,名称:" Barcelona"}显示在控制台中。

实际行为:

未定义显示在控制台

有人可以更正我的代码吗?



<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Prueba de index</title>
	<script src="./bower_components/webcomponentsjs/webcomponents.js"></script>
	<link rel="import" href="./hola-mundo.html">
</header>
<body>

	<hola-mundo></hola-mundo>
</body>
</html>
&#13;
&#13;
&#13;

&#13;
&#13;
<link rel="import" href="./bower_components/polymer/polymer.html">
<link rel="import" href="./hidding-behaviour.html">
<link rel="import" href="./hola-mundo2.html">
<dom-module id="hola-mundo">
	<style>
		h1{
			color: blue;
		}
	</style>
	<template>
		<h1>hello world</h1>
		
		<button on-click="showValueMyobject">showValueMyobject</button>
		<hola-mundo2 myObject={{myObject}}></hola-mundo2>
	</template>

	<script>
		Polymer({
			is: "hola-mundo",		
			properties:{
				myObject: {
					type: Object,
					notify: true,
					observer: "projectChanged"				
				},
			},

			changeValueMyObject: function(){
				let obj ={
					id:2,
					name:'Madrid'
				}
				this.set('myObject',obj)
			},	
			
			showValueMyobject: function(){
				debugger
				console.log(this.myObject);
			},	
		});
	</script>
</dom-module>
&#13;
&#13;
&#13;

&#13;
&#13;
<link rel="import" href="./bower_components/polymer/polymer.html">
<link rel="import" href="./hidding-behaviour.html">
<dom-module id="hola-mundo2">
	<style>
		h1{
			color: blue;
		}
	</style>
	<template>
		<h1>hello world2</h1>
		<button on-click="changeValueMyObject">changeValueMyObject2</button>
		
		
	</template>

	<script>
		Polymer({
			is: "hola-mundo2",			
			properties:{
				myObject: {
					type: Object,
					notify: true
					
				},
			},
			
			changeValueMyObject: function(){
				let obj ={
					id:1,
					name:'Barcelona'
				}
				this.set('myObject', obj);
			},	
			
			showValueMyobject: function(){
				debugger
				console.log(this.myObject);
			},	
			
		});
	</script>
</dom-module>
&#13;
&#13;
&#13;

0 个答案:

没有答案