简体   繁体   English

是否可以在 VueJS(类星体框架)中跨页面使用变量?

[英]Is it possible to use a variable across pages in VueJS (Quasar Framework)?

I am wondering if it's possible to do something like this:我想知道是否可以这样做:

//File1.vue, <template> has a form on it
<script>

export default {
  data () {
    return {
      name: null,
      job: null
    }
  },
  methods: {
    onSubmit () {
      /// Store this.name and this.job in a variable
    },
    onReset () {
      this.name = null
      this.job = null
    }
  }
}
</script>

And access this variable on other.vue page, like this:并在 other.vue 页面访问这个变量,像这样:

<script>
// access job and name here
export default {
}
</script>

Thanks for your time.谢谢你的时间。

One option will be to use local storage:一种选择是使用本地存储:

onSubmit () {
  localStorage.setItem('storedName', this.name)
  localStorage.setItem('storedJob', this.job)
},

Then:然后:

<script>
const storedName = localStorage.getItem('storedName')
const storedJob = localStorage.getItem('storedJob')
export default {

}
</script>

This is what vuex and other state management libraries are designed for.这就是 vuex 和其他 state 管理库的设计目的。
If you need your vaiables to be reactive, while not compilcate too much, you can use simpliest state management approach:如果您需要您的变量是反应性的,而不是过于复杂,您可以使用最简单的 state 管理方法:

// src/stores/submitData.js
import Vue from 'vue';

export default Vue.observable({
    name: null,
    job: null,
});
<!-- inside vue component -->
<script>
import submitData from 'src/stores/submitData.js';

export default {
  data () {
    return {
      name: null,
      job: null
    }
  },
  methods: {
    onSubmit () {
      // Store this.name and this.job in a variable
      submitData.name = this.name;
      submitData.job = this.name;
    },
    onReset () {
      submitData.name = this.name = null;
      submitData.job = this.name = null;
    }
  },
  computed: {
    // currentJob will be updated whenever `submitData.job` changes
    currentJob() {
      return submitData.job;
    }
  }
}
</script>
// You can also use your store outside of vue component context (in normal js modules)
import submitData from 'src/stores/submitData.js';
// ...
const submittedJob = submitData.job;

Provide/inject seems like a suitable solution. Provide/inject似乎是一个合适的解决方案。

In your case it would look like this:在您的情况下,它看起来像这样:

//File1.vue, <template> has a form on it
<script>

export default {
  data () {
    return {
      name: null,
      job: null
    }
  },
  provide: {
    return {
      name: this.name,
      job: this.job
    }
  },
  methods: {
    onSubmit () {
      /// Store this.name and this.job in a variable
    },
    onReset () {
      this.name = null
      this.job = null
    }
  }
}
</script>

with this on other page:在其他页面上有这个:

<script>
import { inject } from 'vue'

const name = inject('name')
const job = inject('job')
export default {
}
</script>

or, to be consistent and use Options API:或者,为了保持一致并使用选项 API:

<script>
export default {
    inject: ['name', 'job']
    created() {
        console.log(this.name) // injected value
    }
}
</script>

Consider using state management.考虑使用state管理。 In the past (Vue 2) it was vuex, now with Vue 3 it is Pinia that is most recommended (Pinia may be the next default state management system for future Vue).以前(Vue 2)是vuex,现在Vue 3最推荐的是Pinia(Pinia可能是未来Vue下一个默认的state管理系统)。

Quasar works very well with Pinia (and vite). Quasar 与 Pinia(和 vite)配合得很好。 Just define that you will use them at the project creation (in the interactive questions): pinia, eslint, i18n, vite (in random order).只需定义您将在项目创建时(在交互式问题中)使用它们:pinia、eslint、i18n、vite(随机顺序)。

Please note that some plugins / tools work with Pinia OR Vuex, not both.请注意,某些插件/工具可与 Pinia 或 Vuex 一起使用,但不能同时与两者一起使用。 Eg.例如。 Vue-auth Vue-auth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM