在 vue js 中将函数作为 props 传递
Passing Functions as props in vue js
我正在尝试将 editTodo 作为 props 函数从父 app.vue 传递给子组件...
TodoItem.vue 组件有一个项目列表和时间 returns 到 newTodo 和 dateTime 字段的主要用户输入。其实我是Vue js的新手,对pass props b/w 组件通讯了解不多
<template>
<div id="app" class="container">
<TodoInput :addTodo="addTodo"
:updateTodo="updateTodo"
/>
<todo-item v-for="(todo, index) in todos"
:key=todo.id
:todo=todo
:index =index
:removeTodo="removeTodo"
:editTodo="editTodo" />
</div>
</template>
<script>
import TodoInput from "./components/TodoInput.vue";
import TodoItem from "./components/TodoItem.vue";
export default {
name: "App",
components: {
TodoInput,
TodoItem,
},
data() {
return {
editing:false,
editItems:{},
todos: [
// {
// id: 1,
// title: "",
// date: new Date(),
// editing: false,
// completed: false,
// },
// {
// id: 1,
// title: "",
// date: new Date(),
// editing: false,
// completed: false,
// },
],
};
},
methods: {
editTodo(index, newTodo, dateTime){
, ' dateTime ', dateTime)
// this.editItems = {
// id,
// title,
// time,
// }
this.todo = newTodo
this.todo = dateTime
this.selectedIndex = index
this.editing = true
},
TodoItem.vue 组件有一个项目列表和时间 returns 到 newTodo 和 dateTime 字段的主要用户输入。***enter code here
`**
-->
{{todo.title}}
{{todo.time}}
</div>
<div class="remove-item" @click="removeTodo(index)">
×
</div>
<div class="edit-item" @click="eiditTodo(index)"
>
<i class="fas fa-edit" id="edit"></i>
</div>
导出默认{
姓名:'todo-item',
道具:{
去做:{
类型:对象,
要求:真实,
},
移除待办事项:{
type:Function,
required:true,
},
指数:{
type:Number,
要求:真实,
},
},
data(){
return{
'id': this.todo.id,
'title': this.todo.newTodo,
'time': this.todo.dateTime,
}
methods:
getEdit(){
this.$emit('editTodo', this.selectedIndex)
}
**`
与其将函数作为 prop 传递给子组件以便 运行 它,不如从子组件发出事件并在父组件中执行该函数。
从子组件发出事件
@click="$emit('edit-todo')"
然后在父组件中
<div @edit-todo="editTodo">
</div>
或者,您可以在 TodoItem
组件中定义 editTodo
函数并直接调用它。
我正在尝试将 editTodo 作为 props 函数从父 app.vue 传递给子组件... TodoItem.vue 组件有一个项目列表和时间 returns 到 newTodo 和 dateTime 字段的主要用户输入。其实我是Vue js的新手,对pass props b/w 组件通讯了解不多
<template>
<div id="app" class="container">
<TodoInput :addTodo="addTodo"
:updateTodo="updateTodo"
/>
<todo-item v-for="(todo, index) in todos"
:key=todo.id
:todo=todo
:index =index
:removeTodo="removeTodo"
:editTodo="editTodo" />
</div>
</template>
<script>
import TodoInput from "./components/TodoInput.vue";
import TodoItem from "./components/TodoItem.vue";
export default {
name: "App",
components: {
TodoInput,
TodoItem,
},
data() {
return {
editing:false,
editItems:{},
todos: [
// {
// id: 1,
// title: "",
// date: new Date(),
// editing: false,
// completed: false,
// },
// {
// id: 1,
// title: "",
// date: new Date(),
// editing: false,
// completed: false,
// },
],
};
},
methods: {
editTodo(index, newTodo, dateTime){
, ' dateTime ', dateTime)
// this.editItems = {
// id,
// title,
// time,
// }
this.todo = newTodo
this.todo = dateTime
this.selectedIndex = index
this.editing = true
},
TodoItem.vue 组件有一个项目列表和时间 returns 到 newTodo 和 dateTime 字段的主要用户输入。***enter code here
`**
--> {{todo.title}} {{todo.time}}</div> <div class="remove-item" @click="removeTodo(index)"> × </div> <div class="edit-item" @click="eiditTodo(index)" > <i class="fas fa-edit" id="edit"></i> </div>
导出默认{ 姓名:'todo-item', 道具:{ 去做:{ 类型:对象, 要求:真实, }, 移除待办事项:{ type:Function, required:true, }, 指数:{ type:Number, 要求:真实, },
},
data(){
return{
'id': this.todo.id,
'title': this.todo.newTodo,
'time': this.todo.dateTime,
}
methods:
getEdit(){
this.$emit('editTodo', this.selectedIndex)
}
**`
与其将函数作为 prop 传递给子组件以便 运行 它,不如从子组件发出事件并在父组件中执行该函数。
从子组件发出事件
@click="$emit('edit-todo')"
然后在父组件中
<div @edit-todo="editTodo">
</div>
或者,您可以在 TodoItem
组件中定义 editTodo
函数并直接调用它。