uni-app vue3 <script setup>语法糖代码写法不支持bug
uni-app vue3 <script setup>语法糖代码写法不支持bug
示例代码:
<template>
<view class="content">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title">{{title}}</text>
</view>
</view>
</template>
<script setup>
import {
getCurrentInstance,
onMounted,
defineExpose,
ref
} from 'vue'
const instance = getCurrentInstance()
const title = 'ssssssffffffff'
const kkkkkkkk = ref('ppppppppppppppppxxxxxxxxxxxxxxxxx')
console.log(instance.proxy.title);
onMounted(() => {
console.log(instance);
})
// export default {
// data() {
// return {
// title: 'Hello'
// }
// },
// onLoad() {
// console.log(this);
// },
// methods: {
// }
// }
// defineExpose({
// title
// })
</script>
操作步骤:
<template>
<view class="content">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title">{{title}}</text>
</view>
</view>
</template>
<script setup>
import {
getCurrentInstance,
onMounted,
defineExpose,
ref
} from 'vue'
const instance = getCurrentInstance()
const title = 'ssssssffffffff'
const kkkkkkkk = ref('ppppppppppppppppxxxxxxxxxxxxxxxxx')
console.log(instance.proxy.title);
onMounted(() => {
console.log(instance);
})
// export default {
// data() {
// return {
// title: 'Hello'
// }
// },
// onLoad() {
// console.log(this);
// },
// methods: {
// }
// }
// defineExpose({
// title
// })
</script>
直接模拟器运行
预期结果:
- 能访问到当前组件实例对象上的已声明的变量
实际结果:
- 访问不到
bug描述:
uniapp app模式下vue3 <script setup>语法糖写法下通过getCurrentInstance.proxy获取当前实例访问不到声明的变量,普通export default {}方式写法去访问this上的属性却可以访问到
更多关于uni-app vue3 <script setup>语法糖代码写法不支持bug的实战教程也可以访问 https://www.itying.com/category-93-b0.html
抱歉,我没打完整,应该是这样通过当前实例去获取getCurrentInstance().proxy,我在vue3里试了可以访问到,没事也是个小问题,反应一下,帮助完善
回复 1***@qq.com: 请问您是如何在vue3里边可以访问到的,我是这样写的访问不到 <script setup> import { getCurrentInstance, onMounted, ref } from ‘vue’ const instance = getCurrentInstance() const title = ref(‘test’) onMounted(() => { console.log(‘title’, instance.proxy.title); }) </script>
在 uni-app 中使用 Vue 3 的 <script setup> 语法糖时,可能会遇到一些兼容性问题或 bug。以下是一些常见的问题及解决方法:
1. defineProps 和 defineEmits 未定义
-
问题描述: 在
<script setup>中使用defineProps或defineEmits时,可能会遇到未定义的错误。 -
解决方法: 确保你的
uni-app项目已经正确配置了 Vue 3 和@vue/compiler-sfc。你可以通过在vite.config.js或vue.config.js中配置compilerOptions来启用这些功能。// vite.config.js import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue'; export default defineConfig({ plugins: [ vue({ script: { defineModel: true, }, }), ], });
2. ref 和 reactive 无法正常工作
-
问题描述: 在
<script setup>中使用ref或reactive时,可能会遇到响应式数据无法正常工作的问题。 -
解决方法: 确保你正确导入了
ref和reactive,并且在模板中正确使用它们。<script setup> import { ref, reactive } from 'vue'; const count = ref(0); const state = reactive({ message: 'Hello, uni-app!', }); </script> <template> <div>{{ count }}</div> <div>{{ state.message }}</div> </template>
3. watch 和 computed 无法正常工作
-
问题描述: 在
<script setup>中使用watch或computed时,可能会遇到无法监听或计算的问题。 -
解决方法: 确保你正确导入了
watch和computed,并且在setup函数中正确使用它们。<script setup> import { ref, watch, computed } from 'vue'; const count = ref(0); watch(count, (newValue, oldValue) => { console.log(`count changed from ${oldValue} to ${newValue}`); }); const doubled = computed(() => count.value * 2); </script> <template> <div>{{ count }}</div> <div>{{ doubled }}</div> </template>
4. <script setup> 中的 this 无法使用
-
问题描述: 在
<script setup>中无法使用this,因为setup函数中没有this上下文。 -
解决方法: 避免在
<script setup>中使用this,直接使用ref、reactive等 Vue 3 提供的 API。<script setup> import { ref } from 'vue'; const message = ref('Hello, uni-app!'); </script> <template> <div>{{ message }}</div> </template>
5. <script setup> 中的 async 函数
-
问题描述: 在
<script setup>中直接使用async函数可能会导致问题。 -
解决方法: 将
async函数放在onMounted或其他生命周期钩子中,或者在setup函数外部定义async函数。<script setup> import { onMounted } from 'vue'; const fetchData = async () => { const response = await fetch('/api/data'); const data = await response.json(); console.log(data); }; onMounted(() => { fetchData(); }); </script>
6. <script setup> 中的 import 问题
-
问题描述: 在
<script setup>中导入组件或工具时,可能会遇到路径问题。 -
解决方法: 确保路径正确,并且使用相对路径或别名来导入组件或工具。
<script setup> import MyComponent from '@/components/MyComponent.vue'; import { myUtility } from '@/utils/myUtility'; </script> <template> <MyComponent /> </template>
7. <script setup> 中的 props 和 emit
-
问题描述: 在
<script setup>中使用props和emit时,可能会遇到类型推断或未定义的问题。 -
解决方法: 使用
defineProps和defineEmits来定义props和emit,并确保在模板中正确使用它们。<script setup> const props = defineProps({ message: String, }); const emit = defineEmits(['update:message']); const updateMessage = () => { emit('update:message', 'New Message'); }; </script> <template> <div>{{ message }}</div> <button @click="updateMessage">Update Message</button> </template>
8. <script setup> 中的 scoped 样式
-
问题描述: 在
<script setup>中使用scoped样式时,可能会遇到样式无法应用的问题。 -
解决方法: 确保在
style标签中正确使用scoped属性,并且在模板中正确使用class或id。<script setup> const message = 'Hello, uni-app!'; </script> <template> <div class="message">{{ message }}</div> </template> <style scoped> .message { color: red; } </style>
9. <script setup> 中的 v-model
-
问题描述: 在
<script setup>中使用v-model时,可能会遇到双向绑定无法正常工作的问题。 -
解决方法: 确保在
props中定义了modelValue,并且在emit中正确触发update:modelValue事件。<script setup> const props = defineProps({ modelValue: String, }); const emit = defineEmits(['update:modelValue']); const updateValue = (event) => { emit('update:modelValue', event.target.value); }; </script> <template> <input :value="modelValue" @input="updateValue" /> </template>
10. <script setup> 中的 provide 和 inject
-
问题描述: 在
<script setup>中使用provide和inject时,可能会遇到无法传递或接收数据的问题。 -
解决方法: 确保在父组件中正确使用
provide,并且在子组件中正确使用inject。<!-- ParentComponent.vue --> <script setup> import { provide } from 'vue'; import ChildComponent from './ChildComponent.vue'; provide('message', 'Hello from Parent!'); </script> <template> <ChildComponent /> </template> <!-- ChildComponent.vue --> <script setup> import { inject } from 'vue'; const message = inject('message'); </script> <template> <div>{{ message }}</div> </template>


