Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Vue.js Development Techniques and Best Practices

Notes 1

Automated Component Registration with Webpack

Leveraging webpack's require.context enables automatic registration of Vue components without manual imports. This approach streamlines the component registration process in main.js:

import Vue from 'vue'
import upperFirst from 'lodash/upperFirst'
import camelCase from 'lodash/camelCase'

const componentModules = require.context(
  './components',
  false,
  /Base[A-Z]\w+\.(vue|js)$/)

componentModules.keys().forEach(filePath => {
  const moduleConfig = componentModules(filePath)
  
  const componentName = upperFirst(
    camelCase(
      filePath
        .split('/')
        .pop()
        .replace(/\.\w+$/, '')
    )
  )

  Vue.component(
    componentName,
    moduleConfig.default || moduleConfig
  )
})

The require.context method accepts three parameters: target directory, subdirectory scanning flag, and filename matching regex.

Inheritance Properties and Event Listeners

$attrs Property

When passing numerous properties from parent to child components, $attrs captures undefined props values:

// Parent component
<home title="Page Title" width="80" height="80" imgUrl="image-source"/>

// Child component
mounted() {
  console.log(this.$attrs) // {title: "Page Title", width: "80", height: "80", imgUrl: "image-source"}
}

Props explicitly defined in the child component are automatically excluded from $attrs.

$listeners Property

For invoking parent methods from child components, $listeners provides access to parent events:

// Parent component
<home @change="handleChange"/>

// Child component
mounted() {
  console.log(this.$listeners) // Contains handleChange event
}

Dynamic Component Management

Implementing tab interfaces requiers dynamic component loading:

<component :is="activeComponent"></component>

Performance optimization through component caching:

<keep-alive>
  <component :is="activeComponent"></component>
</keep-alive>

Adding transition effects:

<transition>
  <keep-alive>
    <component :is="activeComponent"></component>
  </keep-alive>
</transition>

Component Extension Mechanism

Creating component constructors for element mounting:

const UserProfile = Vue.extend({
  template: '<p>{{extendedInfo}}</br>Data passed: {{receivedProps}}</p>',
  data() {
    return {
      extendedInfo: 'Extended component data',
    }
  },
  props: ['receivedProps']
})

// Mount instance with property data
new UserProfile({
  propsData: { receivedProps: 'Instance data' }
}).$mount('#target-element')

// Register as global component
Vue.component('UserProfile', UserProfile)

Asset URL Transformation

Simplified asset handling replaces explicit require statements:

<!-- Before -->
<template>
  <div>
    <avatar :img-src="avatarSource"></avatar>
  </div>
</template>
<script>
export default {
  created() {
    this.avatarSource = require('./assets/default-avatar.png')
  }
}
</script>

<!-- After -->
<template>
  <div>
    <avatar img-src="./assets/default-avatar.png"></avatar>
  </div>
</template>

Configuration for vue-cli 3.x:

module.exports = {
  chainWebpack: config => {
    config
      .module
        .rule('vue')
        .use('vue-loader')
        .tap(options => {
          options.transformAssetUrls = {
            avatar: 'img-src',
          }
          return options;
        });
  }
}

Path Alias Configuration

vue-cli 2.x Setup

resolve: {
  extensions: ['.js', '.vue', '.json'],
  alias: {
    'vue$': 'vue/dist/vue.esm.js',
    '@': resolve('src'),
  }
}

vue-cli 3.x Setup

const path = require('path')

function resolvePath(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  chainWebpack: config => {
    config.resolve.alias
      .set('alias-key', resolvePath('src/components'))
  }
}

Image Error Handling

Implementing fallback images for broken sources:

<img :src="imageUrl" @error="setDefaultImage" alt="">
<script>
export default{
  data(){
    return{
      imageUrl:''
    }
  },
  methods:{
    setDefaultImage(event){
      event.target.src = require('@/assets/default-image.png')
    }
  }
}
</script>

Slot System

Mixin Composition

Component References and Parent Access

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.