Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 app.config.compilerOptions Property

Vue 3 app.config.compilerOptions Property


Vue3 Global API Vue 3 Global API

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

Example

app.config.compilerOptions.isCustomElement = (tag) => tag.startsWith('my-');
The above code treats all tags starting with `my-` as custom elements.

2. whitespace

- `'preserve'`: Preserve all whitespace characters. - `'condense'`: Condense whitespace characters into a single space. - `'collapse'`: Remove all redundant whitespace characters.

Example

app.config.compilerOptions.whitespace = 'condense';
The above code compresses whitespace in the template into a single space.

3. delimiters

Example

app.config.compilerOptions.delimiters = ['${', '}'];
The above code changes the interpolation expression from `{{ }}` to `${ }`.

4. comments

Example

app.config.compilerOptions.comments = true;
The above code preserves HTML comments in the template.

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:


Notes

  1. Applies only to template compilation:app.config.compilerOptions only affects the compilation behavior of Vue templates and does not affect JavaScript code.
  2. 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.
  3. Compatibility of Custom Elements: When using isCustomElement , make sure the definition and usage of custom elements are compatible with Vue.

Vue3 Global API Vue 3 Global API