//ProjectMix.js
和 .vue 写法相同,有data created mounted computed methods等方法。
export default { data() { return { project: {}, projectId: 0, }; }, created() { this.project = this.$store.getters.project; const projectId = this.project.projectId; if (/^[1-9][0-9]*$/.test(projectId)) { this.projectId = projectId; } else { this.backProjects(); } }, methods: { backProjects() { this.$router.push("/project/projects"); } } }
使用方法:
import ProjectMix from "@src/mixins/ProjectMix";//引入 export default { name: "", mixins: [ProjectMix], components: { }, props: { }, data() { } }
ProjectMix引入之后就可以直接使用这个mixins中的方法属性等
比如取projectId就直接this.projectId即可。
this.backProjects()可以直接调用mixins中的backProjects方法。