[英]Laravel compact in vue
我想知道如何将变量传递给 laravel 中的 vue 组件?
当我们使用刀片时,我们可以传递如下变量:
$now = Carbon::now();
return view('xxxxxxxx', compact('now');
这样我就可以在xxxxxxxx
刀片文件中使用$now
了。 但是 vue 组件呢? 我们通常通过 json 为组件返回数据,并且使用 axios 路由我们得到该信息无法为我们的确切组件指定此类数据?
如果我想使用$now = Carbon::now();
在single.vue
组件中?
我怎样才能做到这一点?
这是我想对时间做的事情,因为碳不能使用(基于评论)我想使用moment.js
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()
});
},
}
根据我上面的代码,这里是结果
尝试这个:
这是我的路线,我只是用一些预定义的变量渲染一个视图
Route::get('/', function () {
return view('welcome', [
'now' => Carbon::now(),
'deadline' => Carbon::now()->addHours(2)
]);
});
这是我的视图文件。 在这里,我有一个名为: example-component
的自定义元素。 这就是我使用props
将 PHP 变量传递给 Vue 组件的方式。
并将您的数据传递给窗口,如下所示:
<script>window.data = @json(compact('now', 'deadline'))</script>
这是我的 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>
这是有关computed
和filters
的文档。 您可能永远不要在mounted
的函数中添加过滤器,因为它可能会导致内存泄漏。 这是我添加过滤器的方法。 在您的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'
});
如果你想试试这个,你可以编辑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
]);
});
在此处查看有关加法和减法的Carbon
文档。
如果您在上面的代码中的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.