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

4 回复

试了一下vue3,同样的代码在vue3中也是无法通过getCurrentInstance.proxy获取变量值的,可见通过script setup声明的变量不会挂载到组件实例中。事实上已经可以直接操作变量了,也没必要再通过instance.proxy获取

更多关于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. definePropsdefineEmits 未定义

  • 问题描述: 在 <script setup> 中使用 definePropsdefineEmits 时,可能会遇到未定义的错误。

  • 解决方法: 确保你的 uni-app 项目已经正确配置了 Vue 3 和 @vue/compiler-sfc。你可以通过在 vite.config.jsvue.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. refreactive 无法正常工作

  • 问题描述: 在 <script setup> 中使用 refreactive 时,可能会遇到响应式数据无法正常工作的问题。

  • 解决方法: 确保你正确导入了 refreactive,并且在模板中正确使用它们。

    <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. watchcomputed 无法正常工作

  • 问题描述: 在 <script setup> 中使用 watchcomputed 时,可能会遇到无法监听或计算的问题。

  • 解决方法: 确保你正确导入了 watchcomputed,并且在 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,直接使用 refreactive 等 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> 中的 propsemit

  • 问题描述: 在 <script setup> 中使用 propsemit 时,可能会遇到类型推断或未定义的问题。

  • 解决方法: 使用 definePropsdefineEmits 来定义 propsemit,并确保在模板中正确使用它们。

    <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 属性,并且在模板中正确使用 classid

    <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> 中的 provideinject

  • 问题描述: 在 <script setup> 中使用 provideinject 时,可能会遇到无法传递或接收数据的问题。

  • 解决方法: 确保在父组件中正确使用 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>
回到顶部