vue中的编译作用域

规则:

1. 父级模板里的所有内容都是在父级作用域中编译的;

2. 子模板里的所有内容都是在子作用域中编译的。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>test</title>
</head>
<body>

<div id="app">
  <!--这个 message 会去 vue根实例中找-->
  <h2>我是父组件 {
   
   {message}}</h2>

  <!-- 会查找当前模板下的,就是vue实例下的 isShow,它是true,该组件会显示在页面上-->
  <cpn v-show="isShow"></cpn>
</div>

<template id="cpn">
  <div>
    <!--这个 message 会去 cpn组件中找-->
    <h2>我是子组件 {
   
   {message}}</h2>

    <!--会查找当前模板下的,cpn组件下的 isShow,它是false,该按钮不会显示-->
    <button v-show="isShow">我是子组件的按钮</button>
  </div>
</template>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
  let vm = new Vue({
     
     
    el: "#app",
    data: {
     
     
      message: "hello",
      isShow: true
    },
    components: {
     
     
      cpn: {
     
     
        template: "#cpn",
        data() {
     
     
          return {
     
     
            isShow: false,
            message: "world"
          }
        }
      }
    }
  });
</script>

</body>
</html>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43974265/article/details/112640952