账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    在vue中写了一个组件,在一个.vue文件中引用了两次 ,只有第一个起作用是怎么回事呢?
    27
    0

    <template>
    <div class="app-container">

    <div class="two">    
        <simulative :station='station1' :tmp='tmp1' class="elevator" ></simulative> 
        <simulative :station='station2' :tmp='tmp2' class="elevator" ></simulative> 
    </div>
    

    </div>
    </template>
    // 上面是父组件
    // ---------------------------------------------
    // 下面是子组件 simulative.vue
    <template>
    <div class="runBox">

    <div class="chart">
      <img style="z-index:100" src="./assets/下.png" alt="" width="250" height="750">
      <img id="cage" style="width:100px;height:150px; z-index:200;left: 80px;" src="./assets/3关.png" alt="">
      <img style="z-index:300" src="./assets/上.png" alt="" width="250" height="750">
    </div>

    </div>
    </template>

    <script>
    var len = 530 // 电梯可以运行的像素高度
    var i = 1
    export default {

    props: ['station', 'tmp'],
    data() {
      return {
        grid: null
      }
    },
    mounted() {
      // 每一层的高度
      this.grid = 1.0 * len / this.station
      const tmp_px = (this.grid * this.tmp) + 'px'
      console.log(tmp_px)
      document.getElementById('cage').style.bottom = tmp_px
    },
    watch: {
      tmp: function() {
        const tmp_px = (this.grid * this.tmp) + 'px'
        document.getElementById('cage').style.bottom = tmp_px
        console.log(i++)
      }
    }

    }
    </script>

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 彩虹糖没有糖 普通会员 1楼

      在Vue中,当我们在一个.vue文件中引用另一个.vue文件中的组件时,只有第一个组件会被应用到当前页面中。这是因为Vue应用过程中的各个组件都是按需加载的,而不是按顺序加载的。

      当我们在一个.vue文件中引用一个组件时,Vue会检查该组件是否在组件树中已经存在。如果存在,Vue会优先使用这个已经加载的组件,然后才使用这个未加载的组件。这是因为每个组件都是通过其对应的HTML标签名在组件树中被创建的,且HTML标签名是唯一的。

      如果我们在同一个.vue文件中引用了两次同一组件,Vue会首先检查第一个组件是否已加载。如果已加载,Vue会使用第一个组件,然后才能使用第二个组件。如果两个组件未加载,Vue会尝试从组件树中加载第二个组件,这通常发生在两个组件具有相同组件ID或名称的情况下。

      以下是一个简单的例子,展示如何在一个.vue文件中引用两个不同的组件:

      ```vue

      ```

      在这个例子中,我们有两个组件MyComponent.vuemy-component.vueMyComponent.vue<template>标签中声明了一个DOM元素,其中包含两个子组件<my-component></my-component>。这两个子组件在组件树中都是可见的,因为<my-component></my-component><template>标签中的<div>元素的子元素。

      当我们访问<div>元素时,Vue会首先尝试从组件树中加载<my-component></my-component>组件,如果存在,Vue会使用这个已加载的组件,然后才能使用<my-component></my-component>组件。如果这两个组件都没有加载,Vue会尝试从组件树中加载<my-component></my-component>组件,这通常发生在两个组件具有相同组件ID或名称的情况下。

      所以,只有第一个<my-component></my-component>组件会被应用到当前页面中,这是因为Vue应用过程中的各个组件都是按需加载的,而不是按顺序加载的。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部