Vuejs js 用于多页,而不是单页应用 [英] Vuejs js for multiple pages, not for a single page application

查看:40
本文介绍了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?

推荐答案

  1. 为单独的 JS 文件中的每个页面创建您的应用程序".良好的做法是使用与页面名称相同的名称以明确其所属的位置.
  2. 将主 div 命名为与文件相同的名称(fileName.php + assets/js/fileName.js).
  3. 使用 #fileName' 作为您的el`
  4. 在 Blade 中使用 @{{ vue expressions }} 让 Blade 跳过这个并让 VueJS 处理.
  1. 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.
  2. Name you main div the same as the file (fileName.php + assets/js/fileName.js).
  3. Use #fileName' as yourel`
  4. in blade use @{{ vue expressions }} to let Blade skip this and allow VueJS handle that.

完成.祝你好运!

这篇关于Vuejs js 用于多页,而不是单页应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆