使用父级数据在子级中。

huangapple go评论59阅读模式
英文:

Use data from parent in child

问题

如何在组件 Home 中使用变量 test 的值?

这是包含 Home 的组件。在这里,我通过 websocket 连接获取 test 的值。请注意,我正在使用下面显示的路由器。

<template>
<v-app id="inspire">
<v-navigation-drawer v-model="drawer" app clipped>
<v-list dense>
<v-list-tile to="/">
<v-list-tile-action>
<v-icon>home</v-icon>
</v-list-tile-action>
<v-list-tile-content>
<v-list-tile-title>Home</v-list-tile-title>
</v-list-tile-content>
</v-list-tile>
<v-list-tile to="/settings">
<v-list-tile-action>
<v-icon>settings</v-icon>
</v-list-tile-action>
<v-list-tile-content>
<v-list-tile-title>Settings</v-list-tile-title>
</v-list-tile-content>
</v-list-tile>
</v-list>
</v-navigation-drawer>
<v-content>
<v-container fluid fill-height>
<router-view></router-view>
</v-container>
</v-content>
</v-app>
</template>

<script>
export default {
name: 'App',
data() {
return {
test: '0'
}
},
mounted: function () {
console.log("Starting connection to WebSocket Server")
this.connection = new WebSocket("...")
this.connection.onmessage = function (event) {
this.test = event.data
}
}
}
</script>

这是路由器组件的样子:

import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'

Vue.use(Router)

export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/',
name: 'home',
component: Home
},
]
})

我需要 test 的值的 home 组件如下。

<template>
// 使用 test 变量的模板
</template>

<script>
export default {
data() {
return {
// 一些数据
};
},
}
</script>

由于网站由微控制器托管,应用程序应尽量小。因此,对我来说,导入东西不是最佳选择。

英文:

How can I use the value of the variable test in the component Home?

This is the component which includes Home. Here I get the value of test via a websocket connection. Note that I am using a router as shown below.

 &lt;template&gt;
      &lt;v-app id=&quot;inspire&quot;&gt;
        &lt;v-navigation-drawer v-model=&quot;drawer&quot; app clipped&gt;
    
          &lt;v-list dense&gt;
            &lt;v-list-tile to=&quot;/&quot;&gt;
              &lt;v-list-tile-action&gt;
                &lt;v-icon&gt;home&lt;/v-icon&gt;
              &lt;/v-list-tile-action&gt;
              &lt;v-list-tile-content&gt;
                &lt;v-list-tile-title&gt;Home&lt;/v-list-tile-title&gt;
              &lt;/v-list-tile-content&gt;
            &lt;/v-list-tile&gt;
            &lt;v-list-tile to=&quot;/settings&quot;&gt;
              &lt;v-list-tile-action&gt;
                &lt;v-icon&gt;settings&lt;/v-icon&gt;
              &lt;/v-list-tile-action&gt;
              &lt;v-list-tile-content&gt;
                &lt;v-list-tile-title&gt;Settings&lt;/v-list-tile-title&gt;
              &lt;/v-list-tile-content&gt;
            &lt;/v-list-tile&gt;
    
        &lt;/v-navigation-drawer&gt;
    
        &lt;v-content&gt;
          &lt;v-container fluid fill-height&gt;
            &lt;router-view&gt;&lt;/router-view&gt;
          &lt;/v-container&gt;
        &lt;/v-content&gt;
      &lt;/v-app&gt;
    &lt;/template&gt;
    
    &lt;script&gt;
    export default {
      name: &#39;App&#39;,
      data() {
        return {
          test:&#39;0&#39;
        }
      },
  
    
    mounted: function () {
    
     
          console.log(&quot;Starting connection to WebSocket Server&quot;)
          this.connection = new WebSocket(&quot;...&quot;)
        
        this.connection.onmessage = function (event) {
          this.test = event.data
        }
      }
    
    &lt;/script&gt;

This i how the router component looks like:

import Vue from &#39;vue&#39;
import Router from &#39;vue-router&#39;
import Home from &#39;./views/Home.vue&#39;


Vue.use(Router)

export default new Router({
  mode: &#39;history&#39;,
  base: process.env.BASE_URL,
  routes: [
    {
      path: &#39;/&#39;,
      name: &#39;home&#39;,
      component: Home
    },

  ]
})

The component home in which I need the value of test looks like this.

  &lt;template&gt;
      //template using the test variable
    &lt;/template&gt;
    
    &lt;script&gt;
    
    export default {
      data() {
        return {
          //some data
        };
      },
    
    &lt;/script&gt;

As the website is hosted by a microcontroller, the application should be as small as possible. Therefore importing something is not the best option for me.

答案1

得分: 1

To pass data from parent to child you use props.

In the parent template you use v-bind the prop name and then the value or values from the parent you want to pass. v-bind:test-data="test" The value can be any type from a string to an array or an object.

...
<v-content>
  <v-container fluid fill-height>
     <router-view v-bind:test-data="test"></router-view>
  </v-container>
</v-content>
...

In your child, receive the props. Note the change from kabab-case to camelCase

export default {
   name: 'Home',
   props: ['testData'],
   
   // do something with the testData

      created () {
        console.log(this.testData)
      }

   }
英文:

To pass data from parent to child you use props.

In the parent template you use v-bind the prop name and then the value or values from the parent you want to pass. v-bind:test-data=&quot;test&quot; The value can be any type from a string to an array or an object.

...
&lt;v-content&gt;
  &lt;v-container fluid fill-height&gt;
     &lt;router-view v-bind:test-data=&quot;test&quot;&gt;&lt;/router-view&gt;
  &lt;/v-container&gt;
&lt;/v-content&gt;
...

In your child receive the props. Note the change from kabab-case to camelCase

export default {
   name: &#39;Home&#39;,
   props: [&#39;testData&#39;],
   
   // do something with the testData

      created () {
        console.log(this.testData)
      }

   }



</details>



huangapple
  • 本文由 发表于 2023年5月24日 23:39:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定