vue3项目中组件引入ts/原生js文件并调用其方法遇到的一些问题

基础不扎实呀。。。

question

Q1

首先是关于ts/js的模块化编程
例如一个slide.ts文件:

// 省略一些代码......
// 模块
export function slideInit() {
  new Slide().init();
}
// 非模块,当引入vue组件中使用(比如调用上面的slideInit方法)时候会报错
// error: Couldn't resolve component "default" at "/"
new Slide().init();

vue中使用:

import { slideInit } from './slide';

参考:
JavaScript中的模块化编程:https://blog.csdn.net/tyxjolin/article/details/130462589

Q2

参考:Vue中使用原生js创建DOM,元素样式不生效解决办法:https://blog.csdn.net/qq_45118905/article/details/126840908

在Vue项目中也会遇到需要动态创建DOM的情况,但是采用指定className的方式给创建的DOM元素指定样式不起作用,在调试界面能看到类名被解析,但是样式未加载。

measureTooltipElement = document.createElement('div')
measureTooltipElement.className = "ol-tooltip ol-tooltip-measure"

样式:

.ol-tooltip {
  position: relative;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 4px;
  color: white;
  padding: 4px 14px 4px 8px;
  opacity: 0.7;
  white-space: nowrap;
  font-size: 12px;
}
 
.ol-tooltip-measure {
  opacity: 1;
  font-weight: bold;
}

但是在页面渲染时却不生效:
参考了网上众多方法,有以下几种可能:

  1. Vue组件中样式Style中scoped导致样式局部生效,因为采用scoped每个类渲染后会有一个单独的data-v-xxxx编码格式,防止样式污染,去除即可。
  2. 使用样式穿透,跳过scoped,这样就不用去除scoped造成全局样式污染。Vue2使用/deep/ className,Vue3使用:deep(className),此外还有>>>(可以尝试一下,我这里没有效果)。
// Vue3
:deep(.ol-tooltip) {
  position: relative;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 4px;
  color: white;
  padding: 4px 14px 4px 8px;
  opacity: 0.7;
  white-space: nowrap;
  font-size: 12px;
}
 
:deep(.ol-tooltip-measure) {
  opacity: 1;
  font-weight: bold;
}
// Vue2
/deep/ .ol-tooltip {
  position: relative;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 4px;
  color: white;
  padding: 4px 14px 4px 8px;
  opacity: 0.7;
  white-space: nowrap;
  font-size: 12px;
}
 
/deep/ .ol-tooltip-measure {
  opacity: 1;
  font-weight: bold;
}
  1. 可能是类的优先级,在子类前加上父类,这里时动态创建的类,所以使用了也没效果。
  2. 我解决的办法:重新创建一个CSS/SCSS样式文件,然后在Vue组件中引用该文件即可,将不生效的样式都放在该样式文件中,不需要再去除scoped和deep。
<template>
	...
</template>

<script lang="ts" setup>
  import { slideInit } from './slide';

  onMounted(() => {
    slideInit();
  })
  ...
</script>

// 加了scoped,局部的
<style lang="less" scoped>
  @import url('./index.less');
</style>
// 全局(为了生成DOM的样式能生效专门将其css写在一个单独的css文件)
<style lang="less">
  @import url('./slide.less');
</style>

Q3

项目一般打包的时候会处理文件hash
写的图片路径明明是这个,但项目运行起来却变成另一个,有些还自动转成base64
在这里插入图片描述
遇到这种情况,如果你的ts/js文件中有拼接文件路径的,在单独的纯html+js+css里运行没问题,但放到项目中,因为会打包编译转换,所以路径啥的都变了,自然访问不到图片。
解决办法就是直接引用打包好的图片地址:

import f1 from '@/assets/images/f1.png'
import f2 from '@/assets/images/f2.png'
import f3 from '@/assets/images/f3.png'
import f4 from '@/assets/images/f4.png'

export function xxfun() {
  banners = [
    {
      imageName: f1,
    },
    {
      imageName: f2,
    },
    {
      imageName: f3,
    },
    {
      imageName: f4,
    },
  ];
  ......
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3和TypeScript,父组件可以通过使用`props`和`emit`来调用组件方法。下面是一个简单的示例,展示了如何在父组件调用组件方法: 父组件(ParentComponent.vue): ```vue <template> <div> <ChildComponent :message="parentMessage" @childEvent="handleChildEvent" /> </div> </template> <script lang="ts"> import { defineComponent, PropOptions } from 'vue' import ChildComponent from './ChildComponent.vue' export default defineComponent({ name: 'ParentComponent', components: { ChildComponent }, setup() { const parentMessage = 'Hello from parent' const handleChildEvent = (eventData: any) => { console.log('Received child event:', eventData) // 在这里可以对子组件的事件进行处理 } return { parentMessage, handleChildEvent } as PropOptions<ChildComponent> & {} } }) </script> ``` 子组件(ChildComponent.vue): ```vue <template> <div> <button @click="sendMessage">Send Message</button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ name: 'ChildComponent', props: { message: String, }, methods: { sendMessage() { this.$emit('childEvent', this.message) // 触发名为 'childEvent' 的事件,并传递当前组件的 'message' prop 作为参数 } } }) </script> ``` 在上面的示例,父组件通过`<ChildComponent :message="parentMessage" @childEvent="handleChildEvent" />`来将`parentMessage`传递给子组件,并通过`@childEvent`监听子组件的`childEvent`事件。当子组件触发该事件时,父组件将通过`handleChildEvent`方法处理该事件。在子组件,我们有一个按钮,当点击该按钮时,会触发名为`childEvent`的事件,并将当前的`message` prop作为参数传递给父组件。在父组件,我们通过监听这个事件来处理子组件传递过来的数据。 请注意,这只是一个简单的示例,实际情况可能更复杂。你可以根据具体需求调整代码,以满足你的项目要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值