javascript - How to extract public methods in components with vue-cli
怪我咯
怪我咯 2017-05-19 10:34:30
0
2
891

The question is the same as the title.
Additional description:
The project uses vuex, vue-router, vue-router will load data through ajax every time it switches to one side, and the method of loading data on each page is the same. I tried placing the public method in main.js and then calling it in the component. I found that the method can be called, but due to the asynchronous nature of ajax, the value cannot be transferred back to the submodule. Has anyone encountered such a situation? How to solve it? Thanks!

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
曾经蜡笔没有小新

Return a promise is enough
For example:

main.js
function ajaxMethod() {
    return ajax({
        url: 'xxx',
        data: xxx
    })
}
component:
ajaxMethod().then((result) => {
    console.log(result)
})
漂亮男人


You can use vue-resource, like me. Put the api in a separate file and load it into Vue, then you can solve the problem of writing the same api multiple times

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template