Skip to content

服务端渲染不用vuex可以吗? #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
freemember007 opened this issue Nov 30, 2016 · 3 comments
Closed

服务端渲染不用vuex可以吗? #91

freemember007 opened this issue Nov 30, 2016 · 3 comments

Comments

@freemember007
Copy link

vuex写得太麻烦,store可以用一个全局变量替吗?也就是说不用vuex实现服务端渲染。

@yyx990803
Copy link
Member

可以,但你需要自己实现类似的 prefetch 逻辑

@youngwind
Copy link

@freemember007 我也在探索不用 vuex 实现的方案,请问你找到解决方案了吗?

@linweiwei123
Copy link

linweiwei123 commented Mar 24, 2019

@youngwind @freemember007
store可以用全局对象:

// store.js
import * as service from './service'

var store =  {
    state: {
        message: 'hello',
        heros: [],
    },
    setHerosAction() {
        return service.getHeros()
            .then( resp => {
                // resp需要对上data
                console.log('=====返回的heros', resp.data.data);
                this.state.heros = resp.data.data;
            })
    },
    clearHerosAction(){
        this.state.heros = [];
    }
}


export function createStore() {
    return store;
}

// entry-client.js 把数据(window中)赋值给store.state

let store = createStore();
store.state = merge(store.state, window.__INITIAL_STATE__);

let { app } = createApp(store);
app.$mount('#app', true);

// app.js ,store就是全局对象,赋给data

export function createApp(store) {
    let app = new Vue({
        data: store,
        render: h => h(App)
    });
    return { store, app, App}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants