获取发出的数据后,有没有办法存储发出的数据?

Pan*_*kaj 5 vue.js

我正在使用EventBus.我的代码如下.

app.js

window.EventBus = new Vue();
Run Code Online (Sandbox Code Playgroud)

以下是Emit的代码

EventBus.$emit('emitted', {
    SomeProperty: SomeData,
});
Run Code Online (Sandbox Code Playgroud)

下面是检索发出数据的代码

EventBus.$on('emitted', id => {

});
Run Code Online (Sandbox Code Playgroud)

获取发出的数据后,有没有办法存储发出的数据?我试图搜索是否有任何方法可以在某处保存发出的数据?

Fro*_*dor 1

由于您将 Vue 与 Laravel 一起使用:

    // Your event bus
    const EventBus = new Vue()

    // The main Vue instance for your app
    const app = new Vue({
        // ALL YOUR APP LOGIC GOES HERE...

      methods: {
        fetchData (params) {
          // this is the method you use to fetch data, here you can use a library
          // like axios to query your api and return the result
                return axios.get('/api/someData', { params })
        }
      },
      // Every time your app is loaded, it executes the created() hook to retrieve
      // previously stored data from localStorage.
      // Depending on your needs, you can use mounted() hook instead.
      created () {
        // Attach the listener to store someData in localStorage
        EventBus.$on('someData', data => window.localStorage.setItem('someData', data))

        // check for stored someData
        const someData = window.localStorage.getItem('someData')

        if (someData) {
            // do something with someData
        }
        else {
            // there is no someData stored, so fetch and store ($emit)
            this.fetchData()
            .then(response => {
              if (response.data.someData) {
                EventBus.$emit('someData', response.data.someData)
              }
            })
        }
      }
    })
Run Code Online (Sandbox Code Playgroud)