英文:
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.
 <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-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>
This i how the router component looks like:
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
    },
  ]
})
The component home in which I need the value of test looks like this.
  <template>
      //template using the test variable
    </template>
    
    <script>
    
    export default {
      data() {
        return {
          //some data
        };
      },
    
    </script>
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="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)
      }
   }
</details>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论