刷新页面后,Vue.js返回JSON

时间:2018-09-18 23:53:58

标签: javascript php jquery laravel vue.js

我是vue.js的新手,我已经爱上了它,除了我不喜欢一个丑陋的零件,这是“当我点击刷新时页面返回JSON”

page loads normaly On Refresh Page loads JSON

但是,如果我浏览母版页,即它可以按预期正确加载,但是如果单击“刷新”,则会得到JSON响应。

我在后端使用laravel,在我的控制器中,我返回一个JSON响应(是的,我知道),但是,我有点希望vue.js能够自动为我检测链接并像它那样神奇如果我从母版页转到链接/页面,该怎么办?

这是将项目退回到母版页的控制器的样子

public function index()
{
    //
        $task = TodoTask::orderBy('created_at', 'desc')->paginate(4);
        return \request()->json(200, $task);

}

我对此做了很多研究,并且我从这个链接中了解了 https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations我必须配置.htaccess文件才能这样做。

这是我的.htaccess文件的样子

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

我可能做错了什么?

我的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');


window.Vue = require('vue');
import VueRouter from 'vue-router';

window.Vue.use(VueRouter);

Vue.component('example-component', require('./components/ExampleComponent.vue'));
//Vue.component('tasks', require('./components/TodoTask.vue'));
const example = require('./components/ExampleComponent.vue');
const Samplelink = require('./components/SampleComponent.vue');
const task = require('./components/TodoTask.vue');



const routes = [{
    path: '/example',
    component: example

}, {
    path: '/sample',
    component: Samplelink
}, {
    path: '/tasks',
    component: task
}];

const router = new VueRouter({
    mode: 'history',
    routes
});


const app = new Vue({
    el: '#app',
    router
}).$mount('#app');

0 个答案:

没有答案