简体   繁体   English

Vue.js 根据 Prop 中的值在复选框上应用选中

[英]Vue.js apply checked on a checkbox based on value from Prop

Explaination解释

I'm running Vue.js alongside Laravel and only using it for small components at the moment.我将 Vue.js 与 Laravel 一起运行,目前仅将其用于小型组件。 I have a checkbox which I need the attribute 'checked' to either be true or false depending on the value from the Prop. The prop is being set by Laravel when the component is loaded in.我有一个复选框,我需要属性“已检查”为真或假,具体取决于道具的值。加载组件时,该道具由 Laravel 设置。

When the page loads, the checkbox should be checked depending on the value of the prop.页面加载时,应根据道具的值选中复选框。 So if the prop is set to false, the checkbox should not be selected, if it's true then the it needs to be selected.因此,如果 prop 设置为 false,则不应选中该复选框,如果为 true,则需要选中它。

Am I right in thinking that Laravel should set the value via a prop and then I should control that by binding :checked to that Props value?我是否认为 Laravel 应该通过道具设置值,然后我应该通过绑定:checked到该道具值来控制它?

view.blade.php view.blade.php

<public-toggle status="true"></public-toggle>

PublicToggle.vue公共切换.vue

<template>
    <div>
        <label class="text-xs font-bold uppercase text-grey-700 mr-2"><i class="fa-question-circle fad text-blue-500 text-sm"></i> Public</label>
        <label class="switch">
            <input type="checkbox" v-bind:value="status" v-on:change="updatePublicStatus" v-model="status" v-bind:checked="status">
            <span class="slider round"></span>
        </label>
    </div>
</template>
<script>
import Swal from 'sweetalert2'
const Toast = Swal.mixin({
  toast: true,
  position: 'top-end',
  showConfirmButton: false,
  timer: 3000
})
export default {
    data() {
        return {
            // status: '',
        }
    },
    methods: {
        updatePublicStatus: function() {
            if(this.status == true) {
                Toast.fire({
                    type: 'success',
                    title: 'Public'
                })
            } else {
                Toast.fire({
                    type: 'success',
                    title: 'Private'
                })
            }
        }
    }
}
</script>

If anyone could help me get my head round this, I would be very grateful!如果有人可以帮助我解决这个问题,我将非常感激!

I would try the following way, in the Laravel (Blade) part:我会尝试以下方式,在 Laravel (Blade) 部分:

<public-toggle :status="{{$status}}"></public-toggle>

And in the VueJS component:在 VueJS 组件中:

data(){
// your data
},
props: ['status'],

Remember the CamelCase issue, if the Blade var is something like: 'your-var' in VueJs component will be: 'yourVar'.请记住 CamelCase 问题,如果 Blade var 类似于:VueJs 组件中的“your-var”,则为:“yourVar”。

Hope it helps!希望能帮助到你!

hi first you need to pass the variable props like this嗨首先你需要像这样传递变量道具

<public-toggle :status="true"></public-toggle>

then you have to get it in the vue file like this然后你必须像这样在vue文件中获取它

props:{
 status:Boolean
}

just fix thats and it will work... i think:)只需修复它,它就会起作用......我认为:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM