请求request

  • 小程序(微信、支付宝等)运行在独立的沙箱环境中,没有传统开发服务器的代理概念
  • 所有网络请求必须走小程序自身的 wx.request(微信)或 uni.request(UniApp 封装)API,且请求的域名必须在小程序后台的合法域名列表中配置(HTTPS 限制)。
  • 开发时的本地代理(如 Vite 的 proxy)是小程序无法感知的。

vue3.0 Vite 代理

场景Vite 代理是否生效解决方案
H5 开发✅ 生效直接使用代理配置
小程序开发❌ 无效配置合法域名 + 硬编码/环境变量
生产环境❌ 无效服务器部署 + HTTPS

参数

参数用途示例
data请求体数据(POST/PUT{ name: 'John' }
paramsURL 查询参数(GET{ id: 123 } → ?id=123
header请求头{ 'Authorization': 'Bearer xxx' }
methodHTTP 方法'GET''POST'

国际化

vue国际化看官网,就不重点说明

主要说下nvue官网并不明确,且如果是对象的国际化就有问题

首先看官网

<script>
  import {
    initVueI18n
  } from '@dcloudio/uni-i18n'

  // const messages = {} 此处内容省略,和 vue 全局引入的写法一致

  const { t } = initVueI18n(messages)

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

其实就是

 index.js

import langEn from './en'
import zhHans from './zh-Hans'
 
export default {
    'en': langEn,
    'zh-Hans': zhHans
};
<script>
    import {
	    initVueI18n
	  } from '@dcloudio/uni-i18n'
	
	 import messages from '@/lang/index.js';
	
	 const { t } = initVueI18n(messages)
    
     console.log(t('messages'))
     console.log(t('grid.visibleToAll'))//不可以使用对象
</script>

这种弊端是如果是对象就有问题,无法获取,下面使用借助i18n,通过原生链方法来实现

i18n.js

    import messages from '@/lang/index.js';
	const i18nEnable = false;//自行判断
	let currentLang
	if (i18nEnable) {
		currentLang = uni.getStorageSync('CURRENT_LANG')
	} else {
		currentLang = "zh-Hans"
	}
	let i18nConfig = {
		locale: currentLang, // set locale
		messages // set locale messages
	}
	
	// #ifdef VUE2
	import Vue from 'vue'
	import VueI18n from 'vue-i18n'
	Vue.use(VueI18n)
	const i18n = new VueI18n(i18nConfig)
	// #endif
	
	// #ifdef VUE3
	import {
		createI18n
	} from 'vue-i18n'
	const i18n = createI18n(i18nConfig)
	// #endif

    export default i18n
<script>
    import i18n from '@/lang/i18n.js';
	export default {
		data() {
			return {
				$t:'',
			}
		},
		onLoad() {
			this.$t = function(key) {
				var values = [],
					len = arguments.length - 1;
				while (len-- > 0) values[len] = arguments[len + 1];
			
				return i18n._t.apply(i18n, [key, i18n.locale, i18n._getMessages(), this].concat(values))
			};
            console.log(t('grid.visibleToAll'))//可以使用对象
		}
	}
</script>

更多推荐