vue组件传值:父传子 props传递的类型和写法

props写法方式:

1.指定传递属性名 ,数组形式

props: ['id', 'name','list']  //不限制数据类型 

2.指定传递属性名和数据类型,对象形式

props: 
{ 
	id: Number, 
	name: String,
	isRequired: Boolean,
	list: Array, 
	info: Object, 
	addFn: Function 
}

3.指定属性名、数据类型、必要性、默认值

props: { 
	// 基础的类型检查 (`null` 和 `undefined` 会通过任何类型验证)
    aa: Number,
    // 多个可能的类型
    bb: [String, Number],
    //指定属性名、数据类型、必填、默认值
	cc: { 
			type: String, 
			required: true,
			default: 'mxg'  
		},
    loading: {
      type: Boolean,
      default: false,
    },
   
    rowId: {
      type: String,
      default: "id",
    },
    apiUrl: {
      type: String,
      default: "",
    },
   //对象或数组默认值必须从一个工厂函数获取
    activeMethod: {
      type: Function,
      default: () => {
        return true;
      },
    },
    
    // 具有默认值的数组写法
    tableData: {
      type: Array,
      default: () => [],
    },
    formConfig: {
      type: Object,
      default: () => {},
    },
    //带有默认值的对象
    tablePage: {
      type: Object,
      default: () => ({
        currentPage: 1,
        pageSize: 10,
        totalResult: 0,
      }),
    },
    // 自定义验证函数
    propFn: {
      validator: function (value) {
        // 这个值必须匹配下列字符串中的一个
        return ["success", "warning"].indexOf(value) !== -1;
      },
    },
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,组件组件传递数据是通过props来实现的。而组件组件传递数据,则需要通过自定义事件来实现。 首先,在组件中,通过props属性将数据传递组件。可以在组件标签上使用v-bind指令来动态绑定组件的数据,或者直接在组件标签上写入固定的。 例如,在组件中定义一个名为message的prop,并将一个字符串传递组件: ```vue <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' }; } }; </script> ``` 然后,在组件中,可以通过props属性接收组件传递过来的数据,并在模板中使用该数据。 ```vue <template> <div> <p>{{ message }}</p> <button @click="sendMessageToParent">Send Message to Parent</button> </div> </template> <script> export default { props: ['message'], methods: { sendMessageToParent() { this.$emit('custom-event', 'Message from child'); } } }; </script> ``` 在组件中,可以使用`props`属性声明接收的属性名,然后在模板中直接使用即可。另外,如果需要向组件发送数据,可以使用`$emit`方法触发一个自定义事件,并传递需要发送的数据。 最后,在组件中,可以通过在组件标签上监听自定义事件来接收组件传递的数据。 ```vue <template> <div> <child-component :message="parentMessage" @custom-event="receiveMessageFromChild"></child-component> <p>Message received from child: {{ childMessage }}</p> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentMessage: 'Hello from parent', childMessage: '' }; }, methods: { receiveMessageFromChild(message) { this.childMessage = message; } } }; </script> ``` 这样,就完成了组件组件传递数据的过程。当组件中的按钮被点击时,将会触发`custom-event`事件,并将消息发送给组件组件接收到消息后,将其保存在`childMessage`变量中,并在模板中进行展示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值