[英]How to extend app.js methods/data/watch from laravel blade view file without creating components
我的app.js
是:
import { store } from './store';
const app = new Vue({
el: '#app',
store,
mounted(){
...
},
methods:{
...
}
});
我正在使用laravel并且我不想每次都因为很小的原因创建组件。 如果不制作组件, app.js
将充满方法,这些方法并不是在每个页面中都有用。 这就是为什么我想要一种方法从我的home.blade.php
文件中扩展app.js
@section(scripts)
<script>
...
</script>
@endsection
(不创建任何组件)。
或者,使用*.blade.php
中的<script>.. </script>
更新/添加data:{... }
和methods:{... }
Mixin
在app.js
之外创建mixin.js
文件
export var custom = {
data: {
show: true
},
methods: {
hide: function(){
this.show = false;
}
},
watch: {
...
},
...
};
并且,在app.js
文件中:
import { store } from './store';
import { custom } from './mixin'; //Import mixin
window.app = new Vue({
el: '#app',
mixins: [custom], // Add mixin var here
store,
});
/*
[note: for multiple: import {custom1, custom2, ...} and define [custom1, custom2, ...] ]
*/
更多细节:官方文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.