简体   繁体   English

Vue 中的 Laravel 紧凑型

[英]Laravel compact in vue

I want to know how can I pass variables to vue component in laravel?我想知道如何将变量传递给 laravel 中的 vue 组件?

When we work with blade we can pass variables like:当我们使用刀片时,我们可以传递如下变量:

$now = Carbon::now();
return view('xxxxxxxx', compact('now');

That way I can use $now in xxxxxxxx blade file.这样我就可以在xxxxxxxx刀片文件中使用$now了。 But what about vue components?但是 vue 组件呢? we usually return data by json for components and with axios route we get that info no way to specify such data for exact component of us?我们通常通过 json 为组件返回数据,并且使用 axios 路由我们得到该信息无法为我们的确切组件指定此类数据?

What if I want to use $now = Carbon::now();如果我想使用$now = Carbon::now(); in single.vue component?single.vue组件中?

How can I make that happen?我怎样才能做到这一点?

Update更新

Here is what I want to do with timing as carbon cannot be used (based on comments) I want to use moment.js这是我想对时间做的事情,因为碳不能使用(基于评论)我想使用moment.js

Logic逻辑

  1. Let users bid if project deadline hasn't arrived如果项目截止日期未到,让用户投标
  2. Don't let users bid if project deadline has arrived如果项目截止日期已到,请勿让用户投标

template

<template v-if="`${project.deadline | timeAgo}`">
  pssed (will be replaced by button is just for test)
</template>
<template v-else>
  still have time (will be replaced by button is just for test)
</template>

script

var moment = require('moment');
export default {
        data(){
            return {
                project : '',
            }
        },
        mounted: function() {
            // I found this code by google not sure if is currect!
            Vue.filter('timeAgo', function(value){
                return moment(value) >= fromNow()
            });
        },
}

Based on my code above here is the results根据我上面的代码,这里是结果

一

Try this:尝试这个:

This is my routes, simply I just render a view with some pre-defined variables这是我的路线,我只是用一些预定义的变量渲染一个视图

Route::get('/', function () {
    return view('welcome', [
        'now' => Carbon::now(),
        'deadline' => Carbon::now()->addHours(2)
    ]);
});

And this is my view file.这是我的视图文件。 Here I have custom element named: example-component .在这里,我有一个名为: example-component的自定义元素。 And this is how I passed PHP variables to Vue component using props .这就是我使用props将 PHP 变量传递给 Vue 组件的方式。

And pass your data to window like so:并将您的数据传递给窗口,如下所示:

<script>window.data = @json(compact('now', 'deadline'))</script>

And this is my Vue component file:这是我的 Vue 组件文件:

<template>
    <h1>
        <span v-if="isPassed">This job is passed</span>
        <span v-else>You have to finish this job</span>
        {{ parsedDeadline | timeFromX(parsedNow) }}
    </h1>
</template>

<script>
const moment = require('moment');

export default {
    props: {
        now: {
            type: String,
            default: () => window.data.now.date // since `now` is an object, you need to access the `date` property to get plain string date that moment can parse
        },
        deadline: {
            type: String,
            default: () => window.data.deadline.date // same as above
        }
    },
    computed: {
        parsedNow () {
            return moment(this.now)
        },
        parsedDeadline () {
            return moment(this.deadline)
        },
        isPassed () {
            return this.parsedNow.isAfter(this.parsedDeadline)
        }
    }
}
</script>

Here's the documentation aboutcomputed and filters .这是有关computedfilters的文档。 You may NEVER add a filter in a mounted function since it may leads to memory leak.您可能永远不要mounted的函数中添加过滤器,因为它可能会导致内存泄漏。 Here's how I add my filter.这是我添加过滤器的方法。 In your app.js (assumed you're using default Laravel Vue preset)在您的app.js中(假设您使用的是默认的 Laravel Vue 预设)

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

Vue.component('example-component', require('./components/ExampleComponent.vue'));

Vue.filter('timeFromX', (a, b) => a.from(b)); // TADAAA...!!!

const app = new Vue({
    el: '#app'
});

UPDATE更新

If you want to try this, you may edit the routes/web.php and change the deadline value:如果你想试试这个,你可以编辑routes/web.php并更改deadline

Route::get('/', function () {
    return view('welcome', [
        'now' => Carbon::now(),
        'deadline' => Carbon::now()->subHours(2), // Passed 2 hours ago
        // 'deadline' => Carbon::now()->addHours(2), // In 2 hours
    ]);
});

Checkout the Carbon docs here about addition and subtraction.此处查看有关加法和减法的Carbon文档。

UPDATE II更新二

If you got error in app.js from the code above, maybe your transpiler doesn't know about arrow-parens.如果您在上面的代码中的app.js中出现错误,则可能您的转译器不知道箭头括号。

// Looks like arrow-parens not working, see code below
// Vue.filter('timeFromX', (a, b) => a.from(b)); // TADAAA...!!!

// Change it to this ???
Vue.filter('timeFromX', function (a, b) {
    return a.from(b);
});

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

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