Vue 3 app.config.compilerOptions Property
app.config.compilerOptions is an important configuration option that allows developers to finely control the compilation behavior of a Vue application.
app.config.compilerOptions is the object used in Vue 3 to configure compiler behavior. It belongs to the Vue application instance (app)'s configuration object (config), and is specifically used to control behavior during template compilation. By setting these options, developers can adjust Vue's compiler behavior to meet specific needs.
Common Configuration Options
The following are app.config.compilerOptions some commonly used configuration options in
1. isCustomElement
- Purpose: Used to specify which elements should be treated as custom elements (rather than Vue components).
- Applicable scenarios: When you use third-party custom elements (such as Web Components) in a template, you need to mark them as custom elements to prevent the Vue compiler from treating them as unknown Vue components.
- Example:
Example
2. whitespace
- Purpose: Controls how whitespace characters in templates are handled.
- Optional values:
'preserve': Preserve all whitespace characters.'condense': Condense whitespace characters into a single space.'collapse': Remove all redundant whitespace characters.
- Example:
Example
3. delimiters
- Purpose: Modifies the delimiters of interpolation expressions in templates.
- Default value:
['{{', '}}'] - Applicable scenarios: When you need to avoid syntax conflicts with other template engines (such as Django or Jinja2), you can modify the interpolation delimiters.
- Example:
Example
4. comments
- Purpose: Controls whether HTML comments in the template are preserved.
- Default value:
false(comments are not preserved) - Example:
Example
How to Use app.config.compilerOptions?
Here is a complete usage example showing how to configure app.config.compilerOptions:
Example
import
{
createApp
}
from
'vue'
;
import
App from
'./App.vue'
;
const
app
=
createApp
(
App
)
;
// configure compilerOptions
app.
config
.
compilerOptions
=
{
isCustomElement
:
(
tag
)
=>
tag.
startsWith
(
'my-'
)
,
whitespace
:
'condense'
,
delimiters
:
[
'${'
,
'}'
]
,
comments
:
true
,
}
;
app.
mount
(
'#app'
)
;
In the code above:
- Marks all tags starting with
my-as custom elements. - Compresses whitespace in the template into a single space.
- Changes the interpolation expression delimiters to
${ }. - Preserves HTML comments in the template.
Notes
- Applies only to template compilation:
app.config.compilerOptionsonly affects the compilation behavior of Vue templates and does not affect JavaScript code. - Modify Delimiters with Caution: When modifying the delimiters of interpolation expressions, make sure they do not conflict with the syntax of other template engines or frameworks.
- Compatibility of Custom Elements: When using
isCustomElement, make sure the definition and usage of custom elements are compatible with Vue.
Vue 3 Global API