pnpm install jquery
/
,用于导入和全局注册 jQuery: import { App } from 'vue'
import jQuery from 'jquery'
export default {
install: (app: App) => {
.$ = jQuery
= jQuery
('jQuery', jQuery)
}
}
)中引入该文件,并使用 ()
方法注册 jQuery 插件: import { createApp } from 'vue'
import App from './'
import jQueryPlugin from './'
const app = createApp(App)
(jQueryPlugin)
('#app')
$
或 jQuery
访问 jQuery 对象了:// 其他组件中
export default {
mounted() {
this.$('#myElement').addClass('highlight')
}
}
请注意,Vite 不再需要像 Vue CLI 一样配置全局的 ProvidePlugin,并且由于 Vite 的模块解析方式不同,直接在 Vue 3 中使用 import $ from 'jquery'
是不推荐的。