Vue 2.7 provide/inject not working with <script setup> in most builds

This issue has been created since 2022-11-21.

Version

2.7.14

Reproduction link

stackblitz.com

Steps to reproduce

  1. Create a project with vue-cli
  2. Add provide and inject to Vue components that are using <script setup>
  3. Start the dev server

What is expected?

provide and inject work properly

What is actually happening?

Injected variable is undefined, and the following errors are logged to the console:
provide() can only be used inside setup()
inject() can only be used inside setup() or functional components


Upon further investigation, all builds beside vue.runtime.esm.js do not seem to work.

For our usecase, we have multiple vue instances on a webpage (which is rendered by a template engine on our backend), and props will be passed into each instance from the templates on the rendered webpage. That is why I tried to use the full build and ran into this issue.

craigrileyuk wrote this answer on 2022-11-24

Your problem is that in main.js you're using import Vue from 'vue/dist/vue' to instantiate your application.

Then inside your components, you're importing provide and inject straight from vue.

You need to use same import location, otherwise they can't connect with each other or your main application (hence the errors).

Usually, it's recommended to use an alias in your Vite/Webpack file to resolve vue: "vue/dist/some.vue.build.js" to ensure consistency.

The reason the vue.runtime.esm.js worked was because that's the script that importing straight from vue resolves to.

More Details About Repo
Owner Name vuejs
Repo Name vue
Full Name vuejs/vue
Language TypeScript
Created Date 2013-07-29
Updated Date 2022-12-10
Star Count 201155
Watcher Count 6070
Fork Count 33134
Issue Count 595

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Table caption located at bottom when you have subtables in docx format 3 2022-08-19 2022-11-01
Gitlab: Create Static Sites 3 2022-08-19 2022-11-01
Smooth scrolling causes sidebar to unhide itself 5 2022-08-19 2022-11-01
2 false positives, badges.twitch.tv and ocsp.verisign.com 1 2021-05-09 2022-10-14
File.read, File.write 等が無い 3 2021-12-11 2022-10-17
Doesn't work in project that depends on MDX 2 8 2022-02-11 2022-10-25
Totally lost how to install this... 4 2021-06-18 2022-11-12
[REQUEST] Add support for more characters for SVG formatting 5 2022-09-19 2022-12-10
[BUG] Level Name Repeat 2 2022-09-13 2022-11-06
Follow up from feedback 0 2022-11-23 2022-11-28
OpenGL rendering changes Schematic Editor Background to black 0 2021-02-11 2022-11-03
Inconsistent: Solution does not load 10 2021-09-27 2022-10-19
Connect failed: database "Petter" does not exist 2 2021-09-16 2022-10-11
FEAT: Add an ARM64 Windows edition in download 5 2021-09-12 2022-08-11
Don't lose pull request information on release branch 3 2021-12-20 2022-11-24
Adopt Idempotency-Key header instead of using nonce in body. 0 2021-11-01 2022-12-09
component.mwc.Button: afterSetDisabled() 0 2021-08-12 2022-10-31
examples.component.mwc.button 0 2021-08-12 2022-11-01
[oh-my-bash] when we are pressing up arrow cursor is at starting of line 15 2021-09-22 2022-11-10
'Tensor' object has no attribute '_trt' 0 2022-02-07 2022-11-30
Question 68 ignored channels 1 2019-08-19 2022-10-23
Grafana Reports - Raw data in CSV 0 2022-05-13 2022-12-02
Error in the command for installation on macOs 0 2022-01-11 2022-12-06
Add support for Java/JDK 17/18/19 4 2022-01-22 2022-11-01
[FR] shared_stepper for tilting carriages 19 2022-01-24 2022-10-18
Feature: add mailing list to the readme 2 2021-10-18 2022-10-31
PHP 8.1 Enum support 7 2021-10-21 2022-11-18
[typescript] resolve .js extension when `type: "module"` 0 2022-09-21 2022-12-03
Console graphics font options not working correctly 7 2021-12-03 2022-11-29
Add date, from and till query parameters to the filter the appointments index results by date. 5 2021-09-26 2022-11-23
[3.9.0] InvalidArgumentException Can't use incorrect type 4 2021-03-22 2022-12-02
[QUESTION] Best method to install a fresh instance of Learning Pathways 7 2022-02-28 2022-11-28
Scroll to sent message when bottom of the screen is in view 6 2022-10-19 2022-12-07
Does this work with iOS? Many bugs 4 2022-10-15 2022-11-24
**Release:** hwc-buildpack 3.1.16 2 2021-04-26 2022-10-24
Compilation error when installing canvas:ui 8 2021-01-09 2022-12-08
VM can't suppert Lists Or sets have much numbers。 6 2010-01-23 2022-10-18
Cloning a bigger repo (>100MB) always breaks at some point in the middle, no way to resume 1 2021-12-19 2022-10-18
Parser do not recognize @- browser specific rules 12 2012-10-22 2022-11-29
navConfig not showing up 1 2021-01-21 2022-11-28
Variable not being `null` is not inferred from the switch case. 2 2022-10-19 2022-11-08
Origin policy implementation 0 2022-02-13 2022-09-13
Composite should use eventbus from its content / ItemCountChangeListener not working in Composite 0 2022-05-25 2022-11-14
Release 4.3.0 - Release Candidate 7 - Register 0 2022-05-02 2022-12-06
Version 0.1.3 Release 3 2019-11-08 2022-12-04
market-client 无法init大于32G的订单 4 2022-03-21 2022-08-01
Is this project abandoned? 3 2021-10-08 2022-11-17
Reconnect when the server restarts 5 2021-03-01 2022-12-05
Missing mutations for ListView items 5 2022-08-24 2022-10-11
Flutter FadeInImage _CastError: Null check operator used on a null value 2 2022-09-13 2022-11-27