在第二个组件当中,去改变第一个组件当中的某个属性的值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Vue.js</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<!-- vue-app是根容器 -->
<div id="vue-app">
<h1> 初始化多个vue对象 </h1>
<div id="vue-app-one">
<h2>{{title}}</h2>
<p>{{greet}}</p>
</div>
<div id="vue-app-two">
<h2>{{title}}</h2>
<p>{{greet}}</p>
<button v-on:click="changeOneTitle">change one title</button>
</div>
</div>
<script src="app.js" type="text/javascript"></script>
</body>
</html>
app.js
'use strict';
// 实例化vue对象
var one = new Vue({
el: "#vue-app-one",
data: {
title: "app-one的内容",
// greet: "打招呼"
},
methods: {
},
computed: {
greet: function(){
return "Hello App One";
}
}
});
var two = new Vue({
el: "#vue-app-two",
data: {
title: "app-two的内容"
},
methods: {
changeOneTitle: function(){
one.title="已经改变了";
}
},
computed: {
greet: function(){
return "Hello App Two";
}
}
});
two.title = "app two 的 title 也被改变了"