Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

laravel - Avoid mutating a prop directly for @click

I'm trying to import and use the JetButton component from laravel jetstream. I've added the @click emit event to the jetbutton but getting the error:

Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value.

Component

<template>
    <jet-button @click="update()">
        Update
    </jet-button>
</template>
<script>
   method:{
       update(){ //doing something }
   }
 </script>

JetButton

<template>
    <button @click="$emit('click', $event);" :type="type" class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:shadow-outline-gray transition ease-in-out duration-150">
        <slot></slot>
    </button>
</template>
question from:https://stackoverflow.com/questions/65921886/avoid-mutating-a-prop-directly-for-click

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When emitting an event you need to define the method first which will be called and then emit that event from the method.

The error tells that, don't emit event directly in the prop.

On @click call a method defined in the JetButton component and then use this.$emit('call').

Component

<template>
    <jet-button @call="update">
        Update
    </jet-button>
</template>
<script>
   methods () {
       update(){ //doing something }
   }
 </script>

JetButton

<template>
    <button @click="call" :type="type" class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:shadow-outline-gray transition ease-in-out duration-150">
        <slot></slot>
    </button>
</template>

<script>
methods () {
call () {
this.$emit('call')
}
}
</script>

Handle the event call in the component as <jet-button @call="update"> and then define that method in the methods() section.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...