从组件获取数据到另一个页面视图
Get data from component to another page view
有一个组件 "TableFields.vue",以及两个视图文件 "Home.vue" 和 "Statistics.vue"。
在组件中,我在 data()
对象下有 changes: []
变量。
data () {
return {
startStopA: true,
startStopB: true,
initialValueA: 3,
initialValueB: 3,
randomNumbersArray: [],
randomSignA: '+',
randomSignB: '+',
signsArray: ['+', '-'],
intervalA: null,
intervalB: null,
changes: []
}
},
changes
数组从 calculationsA()
函数动态获取对象。
calculationsA () {
this.randomSignA = this.signsArray[
Math.floor(Math.random() * this.signsArray.length)
]
this.randomSignA === '+'
? (this.initialValueA += this.randomNumbersArray[0])
: (this.initialValueA -= this.randomNumbersArray[0])
const d = new Date()
// console.log(d.toLocaleTimeString())
// console.log(this.randomNumbersArray[0])
// this.changes.push(this.randomNumbersArray[0])
// this.changes.push(d.toLocaleTimeString())
// console.log(this.changes)
const newChange = {}
newChange.field = 'A'
newChange.value = this.randomNumbersArray[0]
newChange.time = d.toLocaleTimeString()
this.changes.push(newChange)
},
如何将 changes: []
从 TableField.vue
组件传递到 Statistics.vue
页面,以便使用 changes
数组对象数据对动态 table 进行编码。我不确定,我是否需要创建新组件,或者没有它也可以完成。基本上,这是出于测试目的而实现的 TableField.vue
组件的工作代码,可以从根 url.
中看到 Home.vue
<div class="statistics">
<table>
<tr>
<th>Field</th>
<th>Value</th>
<th>Time</th>
</tr>
<tr v-for="item in changes" :key="item.value">
<td>{{ item.field }}</td>
<td>{{ item.value }}</td>
<td>{{ item.time }}</td>
</tr>
</table>
</div>
</div>
我需要该代码才能在 Statistics.vue
页面上工作。
为了方便起见,这里是 link 到 gitlab 仓库。
根据我的说法,更好的解决方案应该是使用 Vuex (https://vuex.vuejs.org/),它允许您将数据存储在名为 store 的共享点中。
或者,您可以使用此处记录的事件:https://vuejs.org/v2/guide/components-custom-events.html#Binding-Native-Events-to-Components
您可以在 Statistics.vue
中导入 TableField.vue
并将 changes: []
作为 props 传递。
来自 discord vue chat 的用户 "async" 已经为我解决了这个问题。
解决方案有点复杂,需要更改几个文件。
如果您想了解更多信息,请参阅 gitlab repo,因为它很复杂,我无法在此处记录它。
有一个组件 "TableFields.vue",以及两个视图文件 "Home.vue" 和 "Statistics.vue"。
在组件中,我在 data()
对象下有 changes: []
变量。
data () {
return {
startStopA: true,
startStopB: true,
initialValueA: 3,
initialValueB: 3,
randomNumbersArray: [],
randomSignA: '+',
randomSignB: '+',
signsArray: ['+', '-'],
intervalA: null,
intervalB: null,
changes: []
}
},
changes
数组从 calculationsA()
函数动态获取对象。
calculationsA () {
this.randomSignA = this.signsArray[
Math.floor(Math.random() * this.signsArray.length)
]
this.randomSignA === '+'
? (this.initialValueA += this.randomNumbersArray[0])
: (this.initialValueA -= this.randomNumbersArray[0])
const d = new Date()
// console.log(d.toLocaleTimeString())
// console.log(this.randomNumbersArray[0])
// this.changes.push(this.randomNumbersArray[0])
// this.changes.push(d.toLocaleTimeString())
// console.log(this.changes)
const newChange = {}
newChange.field = 'A'
newChange.value = this.randomNumbersArray[0]
newChange.time = d.toLocaleTimeString()
this.changes.push(newChange)
},
如何将 changes: []
从 TableField.vue
组件传递到 Statistics.vue
页面,以便使用 changes
数组对象数据对动态 table 进行编码。我不确定,我是否需要创建新组件,或者没有它也可以完成。基本上,这是出于测试目的而实现的 TableField.vue
组件的工作代码,可以从根 url.
Home.vue
<div class="statistics">
<table>
<tr>
<th>Field</th>
<th>Value</th>
<th>Time</th>
</tr>
<tr v-for="item in changes" :key="item.value">
<td>{{ item.field }}</td>
<td>{{ item.value }}</td>
<td>{{ item.time }}</td>
</tr>
</table>
</div>
</div>
我需要该代码才能在 Statistics.vue
页面上工作。
为了方便起见,这里是 link 到 gitlab 仓库。
根据我的说法,更好的解决方案应该是使用 Vuex (https://vuex.vuejs.org/),它允许您将数据存储在名为 store 的共享点中。
或者,您可以使用此处记录的事件:https://vuejs.org/v2/guide/components-custom-events.html#Binding-Native-Events-to-Components
您可以在 Statistics.vue
中导入 TableField.vue
并将 changes: []
作为 props 传递。
来自 discord vue chat 的用户 "async" 已经为我解决了这个问题。
解决方案有点复杂,需要更改几个文件。
如果您想了解更多信息,请参阅 gitlab repo,因为它很复杂,我无法在此处记录它。