laravel - 在 Vue 组件中解析 JSON 编码的数据

标签 laravel vue.js

我正在将 json 编码 数据作为 prop 传递到我的 Vue 组件,当我打印整个 prop 变量时,它是显示数据已成功接收,但我无法解析数据。

配置文件.blade.php

@extends('layouts.app')

@section('content')

    <my-profile user-details="{{ json_encode($userDetails) }}"></my-profile>

@endsection

MyProfile.vue

<template>
    <div class="container">
        <div class="row justify-content">
            <div class="col-md-3" id="profile-image">
                <img class="img-fluid rounded" src="https://www.w3schools.com/bootstrap4/paris.jpg" alt="Profile Image">
            </div>            
            <div class="col-md-12">
                <p>{{userDetails}}</p>
                <p> Name:  {{ userDetails.first_name }} </p>
            </div>
        </div>
    </div>
</template>

<style>
#profile-image {
margin-bottom: 30px;
}
</style>

<script>
export default {
props: ["userDetails"]
}
</script>

输出

    {"user_id":2,"first_name":"Shan","last_name":"Biswas","email":"shanpro.2015@gmail.com","phone":"9508168983","created_at":"2019-05-03 05:43:17","updated_at":"2019-05-03 05:43:17"}

    Name:

最佳答案

您需要将值作为动态而非静态绑定(bind)到组件。 因此,将您的 Blade 文件更改为:

@extends('layouts.app')

@section('content')
    <!-- see the colon in front of the props, that is a shortcut for v-bind:prop-name -->
    <my-profile :user-details="{{ json_encode($userDetails) }}"></my-profile>

@endsection

否则所有值都作为简单字符串传递给组件。

关于laravel - 在 Vue 组件中解析 JSON 编码的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55966573/

相关文章:

javascript - 带有 v-model 的 Vuex 输入不是 react 性的

php - 当数据库中存储在缓存中的相同数据的值发生更改时,如何更新缓存值

php - 如何将使用 eloquent 模型从数据库获取的 $users 值转换为 $finalVaue?

javascript - 如何访问组件中的 vuejs webpack 配置变量?

javascript - 如何将 AJAX 数据从 Vue 实例传递到组件?

vue.js - Vue.js 中 $set 的反义词是什么?

php - laravel 文件不存在或不可读

Laravel:使用队列每小时发送 1000 封电子邮件

php - 在 Laravel 中找不到类 'Symfony\Component\Debug\ExceptionHandler'

javascript - 在 Nuxt.Js 中为 document.title 添加一个观察者