vuex 在 uniapp vue2 和 vue3 中的使用(纯代码)

VUE2----------------------------------------------------------------------------------------------------------------------------
index.js页面
这个页面是需要在项目根目录下自己新建一个store文件夹,然后在自己新增一个index.js文件的
如图:
在这里插入图片描述

import Vue from 'vue'
import Vuex from 'vuex'

// 把Vuex作为插件,引入到Vue全局实例中
Vue.use(Vuex)

export default new Vuex.Store({
	state:{
		userName:'张三',
		age:25
	}
})

main.js页面

 //检查是否有vuex
 //import vuex from 'vuex'
 //console.log(vuex)

import App from './App'
// 1 引入Vuex路径
import store from './store'

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
  ...App,
  // 2 实例化
  store
  
})
app.$mount()
// #endif


// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif

index.vue页面

<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
		<view class="text-area">
			<text>{{this.$store.state.userName}}</text>
		</view>
		<view class="text-area">
			<text>你已经</text>
			<text>{{this.$store.state.age}}</text>
			<text>岁啦</text>
		</view>
	</view>
</template>

<script>
	
	export default {
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {

		},
		methods: {

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

VUE2结果
在这里插入图片描述

VUE3----------------------------------------------------------------------------------------------------------------------------
index.js页面
这个页面是需要在项目根目录下自己新建一个store文件夹,然后在自己新增一个index.js文件的
如图:
在这里插入图片描述

import {createStore} from 'vuex'

const store = createStore({
	state:{
		userName:'李四',
		age:20
	}
})
export default store

main.js页面

// 检查是否有vuex
//import vuex from 'vuex'
//console.log(vuex)

// #ifndef VUE3
import Vue from 'vue'
import App from './App'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
    ...App,
})
app.$mount()
// #endif

// #ifdef VUE3
// 1 引入Vuex路径
import store from './store'
import { createSSRApp } from 'vue'
import App from './App.vue'
export function createApp() {
  const app = createSSRApp(App)
  // 2 实例化
  app.use(store)
  return {
    app
  }
}
// #endif

index.vue页面

<template>
	<view>
		<input type="text" value="" placeholder="请输入... ..."/>
	</view>
	<view>
		<text>{{this.$store.state.userName}}</text>
	</view>
	<view>
		<text>你已经</text>
		<text>{{this.$store.state.age}}</text>
		<text>岁了</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			
		}
	}
</script>

<style>

</style>

VUE3结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值