| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <div id="app">
- <h1>num1:{{num1}}</h1>
- <!-- 组件中的变量只能在组件的模板中使用 -->
- <h1>num2:{{num2}}</h1>
- <h1 @click="clickFun">这是一个vue实例的h1标题</h1>
- <box></box>
- </div>
- <template id="box-temp">
- <div>
- <h1>{{num2}}</h1>
- <h1 @click="clickHandle">这是一个组件的h1标题</h1>
- </div>
- </template>
- <script src="./js/vue.js"></script>
- <script>
- new Vue({
- el: '#app',
- data:{
- num1:10
- },
- methods:{
- clickFun(){
- console.log("父组件h1被点击了");
- this.num1++;
- }
- },
- components:{
- "box":{
- // 组件data是一个函数且返回一个对象
- data(){
- return{
- num2:20
- }
- },
- template:"#box-temp",
- methods:{
- clickHandle(){
- console.log("子组件h1被点击了");
- this.num2++;
- }
- }
- }
- }
- })
- </script>
- </body>
- </html>
|