简体   繁体   English

如何正确地将道具从 laravel 8 刀片传递到 vue 3 组件

[英]how to properly pass prop from laravel 8 blade to vue 3 component

I'm trying to get the value of user prop as in the blade file when the App component is mounted but I get undefined in the console.当安装 App 组件但我在控制台中未定义时,我正在尝试获取刀片文件中的 user prop 的值。 I was wondering if something changed in Vue 3 because it worked in Vue 2 .我想知道Vue 3中是否发生了一些变化,因为它在Vue 2中有效。

How can I get the value of the user prop?如何获得用户道具的价值?

laravel.blade file laravel.blade文件

@extends('layouts.app')

@section('content')
    <App 
    @if(auth()->check()) 
        :user="1"
    @endif> 
    </App>
@endsection

App.vue应用程序.vue

<template>
    <div> {{user}} </div>
</template>

<script>
export default {
    name: 'App',

    props: [ 'user'],

    mounted(){
        console.log(this.user)
    }
}
</script>

app.js应用程序.js

import { createApp } from 'vue';
import App from './components/App'
import router from './router';

require('./bootstrap'); 
const app = createApp(App)
app.use(router).mount("#app")

From the migration docs:从迁移文档:

The propsData option has been removed. propsData 选项已被删除。 If you need to pass props to the root component instance during its creation, you should use the second argument of createApp如果您需要在创建根组件实例时将 props 传递给它,您应该使用 createApp 的第二个参数

const app = createApp(
  {
    props: ['username'],
    template: '<div>{{ username }}</div>'
  },
  { username: 'Evan' }
)

It's not as convenient as the Vue 2 way, and I haven't found a good work-around of sending data from your Laravel controller directly to your Vue component.它不如 Vue 2 方式方便,而且我还没有找到一个很好的解决方法,可以将数据从 Laravel controller 直接发送到 Vue 组件。 Best bet may be to get the info by querying your API in the created() method in Vue.最好的办法可能是通过在 Vue 的 created() 方法中查询您的 API 来获取信息。

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

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