vue父子组件传值

15 篇文章 0 订阅
9 篇文章 0 订阅

1.父组件向子组件传值

采用prop down 属性向下传递的方式
1.在父组件中通过子组件标签声明属性的方式传递数据
2.在子组件中声明props接收父组件传递给自己的数据

app.js

const template=
	`
		<div>
		<section class="todoapp">
			<todo-header v-on:addTodo="addTodo"></todo-header>
			<todo-list v-bind:todos="todos"></todo-list>
			<todo-footer></todo-footer>
		</section>
		<app-footer></app-footer>
		</div>
	`;

	const todos=[
		{
			id:1,
			title:'吃饭',
			completed:true
		},
		{
			id:2,
			title:'睡觉',
			completed:false
		},
		{
			id:3,
			title:'写代码',
			completed:false
		}
	];

todo-list.js

const template=
    `
    <section class="main">
    <input id="toggle-all" class="toggle-all" type="checkbox">
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
        <li class="todo" v-for="item in todos">
            <div class="view">
                <input class="toggle" type="checkbox" checked>
                <label>{{item.title}}</label>
                <button class="destroy"></button>
            </div>
            <input class="edit" value="Create a TodoMVC template">
        </li>
    </ul>
    </section>
    `;

    window.todoList={
        template,
        props:['todos']
    }

2.子组件向父组件传值

1.在父组件中定义一个方法(纯业务)
2.在子组件内部调用父组件的方法(不能)
在子组件中发布一个自定义事件,通知父组件你可以去添加任务了
3.在父组件使用子组件的标签上订阅子组件内部发布的自定义事件

app.js

methods:{
			addTodo(titleText2){
				const titleText=titleText2.trim();
				if(!titleText.length){
					return ;
				}
				const todos=this.todos;
				todos.push({
					id:todos[todos.length-1].id+1,
					title:titleText,
					completed:false
				})
			}
		}

todo-header.js

 const template=
    `
    <header class="header">
    <h1>todos</h1>
    <input @keydown.enter="handleKeydown" class="new-todo" placeholder="What needs to be done?" autofocus>
    </header>
    `;
    
    window.todoHeader={
        template,
        methods:{
            handleKeydown(e){
                const target=e.target;
                const value=target.value.trim();
                if(!value.length){
                    return ;
                }
                //子组件中数据已经准备就绪,可以交给父组件使用了
                console.log(this);
                this.$emit('addTodo',value);
                target.value='';
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一种流行的JavaScript框架,用于构建用户界面。在Vue父子组件之间传递数据是一种常见的需求。以下是一种常用的方法来实现父子组件之间的数据传递: 1. Props(属性):组件可以通过props属性向子组件传递数据。在组件,通过在子组件标签上绑定属性的方式传递数据。在子组件,可以通过props选项接收并使用这些数据。 组件: ```html <template> <div> <child-component :message="parentMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentMessage: 'Hello from parent component' }; } } </script> ``` 子组件: ```html <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { props: ['message'] } </script> ``` 2. $emit(自定义事件):子组件可以通过$emit方法触发自定义事件,并将需要传递的数据作为参数传递给组件。在组件,通过在子组件标签上监听自定义事件的方式接收数据。 组件: ```html <template> <div> <child-component @child-event="handleChildEvent"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { handleChildEvent(data) { console.log(data); // 在这里处理子组件传递的数据 } } } </script> ``` 子组件: ```html <template> <div> <button @click="emitEvent">触发事件</button> </div> </template> <script> export default { methods: { emitEvent() { this.$emit('child-event', 'Hello from child component'); // 触发自定义事件,并传递数据给组件 } } } </script> ``` 以上是Vue实现父子组件之间传递数据的两种常用方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值