logo

该视频仅会员有权观看

立即开通课程「Vue.js前端应用开发实践」权限。

¥
199
/ 年

Mixin:制作一个设置页面标题的混合

  • Maxson is a feature that allows mixing components to create a global mix that can blend into all components of an application.
  • The goal is to create a mix to set page titles dynamically, as currently all pages have the project name as the title.
  • A title option is added to the post index component, which can be an attribute or a method that sets the page title.
  • To make the title effective, a new mix is created which fetches the title option from the component, and sets the page title.
  • An environment variable VUE_APP_APP_NAME is added to dev and prod files and then to app config to set the base application name.
  • The title mixin object is created, which contains component lifecycle hooks like created, that outputs "title mix" on the console for global application components.
  • The mixin defines a setTitle method that sets the page title by extracting the title from the component options; if title is a function, it executes it.
  • The page title in the document is set to the value of title followed by the application name.
  • The home page title is treated specially by setting it to app name if the route is the root of the application.
  • When navigating the application, page titles reflect content-specific titles or the application name based on the component's title option.