Vuejs js for multiple pages, not for a single page application(Vuejs js 用于多页,而不是单页应用)
问题描述
我需要使用 laravel 5.3 和 vuejs 2 构建一个应用程序,因为我需要使用双向绑定而不是使用 jquery.
I need to build an application using laravel 5.3 and vuejs 2, because I need to use two-way binding rather than use jquery.
我需要使用刀片模板设置视图.然后,我需要在每个页面中使用 vuejs,如下所述.
I need to set up the views with blade templates. Then, I need to use vuejs in each page as mentioned below.
resources/asserts/js/components/List.vue
resources/asserts/js/components/List.vue
<script>
const panel = new Vue({
el: '#list-panel',
name: 'list',
data: {
message: 'Test message'
},
methods: {
setMessage: function(){
this.message = 'New message';
}
}
})
</script>
resources/asserts/views/post/index.blade.php
resources/asserts/views/post/index.blade.php
<div id="panel" class="panel panel-default">
<div class="panel-heading">Posts</div>
<div class="panel-body">
<p>{{ message }}</p>
<button v-on:click="setMessage">SET</button>
</div>
</div>
有 Add.vue 到 create.blade.php 等等...在 Add.vue el: '#add-panel'
There is Add.vue to create.blade.php etc...
In Add.vue el: '#add-panel'
这是我的 app.js.我已经将默认代码注释如下.
This is my app.js. I already commented default code like follows.
Vue.component('list', require('./components/List.vue'));
Vue.component('add', require('./components/Add.vue'));
// const app = new Vue({
// el: '#app'
// });
我几乎没有检查过大多数文档和教程.但他们使用单个 js 文件.他们将组件用于带有模板的小元素,而不仅仅是 js.
I hardly checked most of documentations and tutorials. But they use a single js file. They use components for small elements with template, not only js.
可以这样使用 vuejs 吗?我需要使用 app.js.最好的方法是什么?
Is it possible to use vuejs this way? Do I need to use app.js. What is the best way to do this?
推荐答案
- 为单独的 JS 文件中的每个页面创建您的应用程序".良好的做法是使用与页面名称相同的名称以明确其所属的位置.
- 将主 div 命名为与文件相同的名称(fileName.php + assets/js/fileName.js).
- 使用
#fileName' 作为您的
el` - 在 Blade 中使用
@{{ vue expressions }}
让 Blade 跳过这个并让 VueJS 处理.
- Create your 'app' for every page in seperate JS files. Good practice would be using the same name as page name to get it clear where it belongs.
- Name you main div the same as the file (fileName.php + assets/js/fileName.js).
- Use
#fileName' as your
el` - in blade use
@{{ vue expressions }}
to let Blade skip this and allow VueJS handle that.
完成.祝你好运!
这篇关于Vuejs js 用于多页,而不是单页应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Vuejs js 用于多页,而不是单页应用


- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01