当前位置:   article > 正文

vue中常用到的一些解决问题方法(整理一些真正有效能够使用到项目中的方法)(等待不断更新积累)_has no default export.vetur

has no default export.vetur

目录

创建项目:参考如下链接

下拉选择框,点击下拉后,竟然会出现我保存在浏览器的输入记录,导致我的下拉列表被遮挡

vue中使用多个class

获取路由参数

父组件传值给子组件

 子组件传值给父组件

内联样式

动态绑定style

vue点击事件如何传参

 加载路由时报错 net::ERR_ABORTED 500 (Internal Server Error)     Failed to fetch dynamically imported module

引入ts

ts报错:Module ‘“xx.vue“‘ has no default export.Vetur

根据路由跳转参数,动态改变导航栏标题

报错:set operation on key failed :target is readonly

vue 3 ref和reactive定义的数据如何修改

跨域

报错:failed to load module script

vite打包之后变量丢失

input输入框,实际值和显示值不一致

vue3中监听值的变化

keep-alive缓存页面

  • 创建项目:参考如下链接

安装 | Vue CLI

  • 下拉选择框,点击下拉后,竟然会出现我保存在浏览器的输入记录,导致我的下拉列表被遮挡

  autocomplete="new-password"

<el-select v-model="province" autocomplete="new-password"></el-select>
  • vue中使用多个class

<span class="class1 class2">多个class</span>
  • 获取路由参数

  1. watch: {
  2. //第一种方法
  3. $route:{
  4. handler(newRouter){
  5. console.log(newRouter.query.xxx)
  6. }
  7. },
  8. //第二种方法
  9. '$route.query.xxx'(Val) {
  10. console.log(Val)
  11. },
  12. },
  • 父组件传值给子组件

父组件

  1. <template>
  2. <div>
  3. <ntable
  4. :getdata="getdata"
  5. :mockData="mockData"
  6. ></ntable>
  7. </div>
  8. </template>
  9. <script>
  10. ...
  11. export default {
  12. data(){
  13. return{
  14. mockData:{
  15. name:'张三',
  16. age:'12'
  17. }
  18. }
  19. },
  20. components:{
  21. ...,
  22. },
  23. methods:{
  24. getdata(){
  25. return this.mockData;
  26. },
  27. },
  28. }
  29. </script>

子组件

  1. <template>
  2. <div>
  3. <button @click="getParentData">{{mockData.name}}</button>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. props:{ // 在props中接收父组件传过来的值
  9. getdata:{
  10. type:Function
  11. },
  12. mockData:{
  13. type:Object,
  14. default:()=>{
  15. return {};
  16. }
  17. }
  18. },
  19. methods:{
  20. getParentData(){
  21. var data = [];
  22. this.getdata(); // 父组件传过来的方法在这里调用
  23. },
  24. }
  25. }
  26. </script>
  •  子组件传值给父组件

vue2

父组件

  1. <template>
  2. <div>
  3. <ntable
  4. @setTableData="setableData"
  5. ></ntable>
  6. </div>
  7. </template>
  8. <script>
  9. import ntable from '@/components/ntable'
  10. export default {
  11. data(){
  12. return{
  13. actionFun:undefined,
  14. }
  15. },
  16. components:{
  17. ntable,
  18. },
  19. methods:{
  20. setableData(data){
  21. this.actionFun = data;
  22. },
  23. },
  24. }
  25. </script>

子组件

  1. <template>
  2. <div>
  3. </div>
  4. </template>
  5. <script>
  6. export default {
  7. mounted(){
  8. this.$emit('setTableData',this.setTableData);/* 向外暴露set方法 */
  9. },
  10. methods:{
  11. setTableData(data){
  12. // do something
  13. },
  14. }
  15. }
  16. </script>

 vue3

父组件

  1. <template>
  2. <div>
  3. <children-dom :show="show" @close="close"></children-dom>
  4. </div>
  5. </template>
  6. <script>
  7. import childrenDom from '@/components/childrenDom'
  8. import { ref,} from 'vue'
  9. const show = ref(false);
  10. const close=()=>{
  11. show.value = false
  12. }
  13. </script>

子组件

  1. <template>
  2. <div @click="confirm">
  3. </div>
  4. </template>
  5. <script setup>
  6. const emit = defineEmits(['close']) //接收父组件数据
  7. defineProps({
  8. show: {
  9. type:Boolean,
  10. default:false,
  11. }
  12. })
  13. const confirm=()=>{
  14. emit('close')
  15. }
  16. </script>

 vue3之语法糖script setup的父子组件、兄弟组件传值_怡暘的博客-CSDN博客

  • 内联样式

  1. <template>
  2. <div :style="{'height':rightHeight+'px','overflow':'auto'}">
  3. </div>
  4. </template>
  5. <script>
  6. export default {
  7. data(){
  8. return{
  9. rightHeight:111,
  10. }
  11. },
  12. }
  13. </script>
  • 动态绑定style

  1. <div :style="[{'background':`url(${userImg}) no-repeat center`},{'background-size': 'cover'}]"></div>
  2. <div :style="{width:videoBox.width+ 'px',height:videoBox.height+ 'px'}"></div>
  3. <div :style="{'border-bottom':index>1?'1px solid red':'none','padding-bottom':index>1?'1px':'0'}"></div>
  • vue点击事件如何传参

  1. <template>
  2. <div @click="say($event,'hi')">Say hi</div>
  3. </template>
  4. <script>
  5. methods:{
  6. say(e,value){
  7. console.log(value)
  8. }
  9. }
  10. </script>
  •  加载路由时报错 net::ERR_ABORTED 500 (Internal Server Error)     Failed to fetch dynamically imported module

重新启动项目npm run dev即可 

  • 引入ts

npm i typescript -save
  1. <script lang="ts">
  2. export default {
  3. data() {
  4. const item = {
  5. date: "2016-05-02",
  6. name: "王小虎",
  7. };
  8. return {
  9. tableData: Array(20).fill(item),
  10. };
  11. },
  12. methods: {
  13. },
  14. };
  15. </script>
  • ts报错:Module ‘“xx.vue“‘ has no default export.Vetur

卸载vetur,安装volar插件,重启下VS Code

  • 根据路由跳转参数,动态改变导航栏标题

router.js文件内

 页面跳转处

  • 报错:set operation on key failed :target is readonly

 正确写法:

 之前报错的错误写法

  • vue 3 ref和reactive定义的数据如何修改

  1. const a = ref('add');
  2. const b = reactive({data:{}})
  3. const getData=()=>{
  4. //修改a的字符串
  5. a.value = 'edit'
  6. //修改b的对象值
  7. b.data = {
  8. name:'小明',
  9. age:12
  10. }
  11. }

  • 跨域

  • 报错:failed to load module script

 是由于当前页面过久没刷新,而其他人已经重新打包发布

解决办法:给一个提示,让用户刷新页面

  • vite打包之后变量丢失

VUE3在本地运行的时候正常,打包之后却显示变量未定义

通过defineProps传进来的变量,在当前页面被修改,需要额外定义一个变量去设置修改的值。如果传进来的值只是作为展示,那么不需要额外设置一个值

 

  • input输入框,实际值和显示值不一致

  • vue3中监听值的变化

  1. // 监听单个数据的变化
  2. const state = reactive({a: 1,b:2})
  3. watch(
  4. ()=> state.a,
  5. (newValue, oldValue)=> {
  6. // ... 微队列调用
  7. },
  8. options // 配置参数对象 如immediate
  9. )
  10. const count = ref(0)
  11. watch(
  12. count,
  13. (newValue, oldValue)=> {
  14. // ...
  15. },
  16. options // 配置参数对象 如immediate
  17. )
  18. // 监听多个数据的变化
  19. watch(
  20. [()=> state.a, count],
  21. ([newValue1, newValue2], [oldValue1, oldValue2])=> {
  22. // ...
  23. },
  24. options
  25. )
  • keep-alive缓存页面

1、vue2中使用keep-alive
将“router-view”组件包含于“keep-alive”即可

  1. <keep-alive>
  2.       <router-view />
  3. </keep-alive>

此时组件将保留状态,或避免重新渲染。

2、vue3中使用keep-alive
vue3的keep-alive应用相对于vue2有所变化,此处描述vue3时如何使用,详情可见:Vue Router文档

 

 

 

  1. <router-view v-slot="{ Component }">
  2.   <transition>
  3.     <keep-alive>
  4.       <component :is="Component" />
  5.     </keep-alive>
  6.   </transition>
  7. </router-view>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/198702
推荐阅读
相关标签
  

闽ICP备14008679号