Vue Area Linkage Picker
vue-area-linkage is a Vue.js 2 component library providing province, city, and district linkage selection pickers. It offers both `AreaSelect` (dropdowns) and `AreaCascader` (cascading menu) components for geographical area selection within China. The current stable version is 5.1.0, and releases appear to be driven by feature enhancements, bug fixes, and updates to its underlying `area-data` dependency, without a fixed cadence. A key differentiator since v5.0.0 is that it no longer bundles area data internally, requiring users to explicitly pass `area-data` through the `data` prop, which significantly reduces bundle size and allows for more flexible, on-demand data loading. Earlier versions (pre-v4) supported street-level selection, which has since been removed.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'pca')
cause `area-data` not installed or not passed to the component.fixInstall `area-data` (`npm i area-data`) and ensure it's imported and bound to the `:data` prop, e.g., `<area-select :data="pca"></area-select>`. -
Component is not registered (e.g., `<AreaSelect>` not recognized)
cause The component was not properly registered globally or locally.fixGlobally: `import VueAreaLinkage from 'vue-area-linkage'; Vue.use(VueAreaLinkage);`. Locally: `import { AreaSelect } from 'vue-area-linkage'; components: { AreaSelect }`. -
Styles are missing or components appear unstyled.
cause The required CSS file has not been imported.fixAdd `import 'vue-area-linkage/dist/index.css';` to your main JavaScript entry file or an appropriate global stylesheet. -
Error: "[Vue warn]: Failed to mount component: template or render function not defined." (or similar related to `vue-area-linkage` in Vue 3)
cause `vue-area-linkage` is designed for Vue 2 and is not compatible with Vue 3.fixThis library is for Vue 2 applications only. For Vue 3, you will need to find a different area linkage component or migrate your project to Vue 2 if this component is critical.
Warnings
- breaking Starting with v5.0.0, the package no longer bundles geographical data. Users must explicitly install `area-data` and pass the data (e.g., `pca` or `pcaa`) to the components via the `:data` prop.
- breaking Version 4.0.0 removed support for street-level selection, limiting components to province, city, and district levels only. The `level` prop no longer accepts a value of 3.
- breaking The `isLinkage` property on `area-select` was renamed to `disableLinkage` in v5.1.0.
- gotcha The CSS styles are not automatically bundled with the components and must be imported separately to ensure proper rendering.
- breaking Version 2.0.0 removed the dependency on `element-ui`, which significantly reduced the bundle size but might require adjustments if your project implicitly relied on `element-ui`'s styles or components being present through `vue-area-linkage`.
Install
-
npm install vue-area-linkage -
yarn add vue-area-linkage -
pnpm add vue-area-linkage
Imports
- VueAreaLinkage
import { VueAreaLinkage } from 'vue-area-linkage';import VueAreaLinkage from 'vue-area-linkage'; Vue.use(VueAreaLinkage);
- { AreaSelect, AreaCascader }
import AreaSelect from 'vue-area-linkage/dist/lib/AreaSelect';
import { AreaSelect, AreaCascader } from 'vue-area-linkage'; - CSS Styles
import 'vue-area-linkage/dist/index.css';
- { pca, pcaa }
import { pca, pcaa } from 'area-data';
Quickstart
import Vue from 'vue';
import { pca, pcaa } from 'area-data';
import 'vue-area-linkage/dist/index.css';
import VueAreaLinkage from 'vue-area-linkage';
Vue.use(VueAreaLinkage);
export default {
data() {
return {
selectedProvinceCity: [],
selectedProvinceCityArea: []
};
},
methods: {
handleSelectChange(value) {
console.log('AreaSelect value changed:', value);
},
handleCascaderChange(value) {
console.log('AreaCascader value changed:', value);
}
},
template: `
<div>
<h2>Province-City Selection</h2>
<area-select v-model="selectedProvinceCity" :data="pca" @change="handleSelectChange"></area-select>
<p>Selected: {{ selectedProvinceCity }}</p>
<h2>Province-City-Area Selection</h2>
<area-cascader v-model="selectedProvinceCityArea" :data="pcaa" @change="handleCascaderChange"></area-cascader>
<p>Selected: {{ selectedProvinceCityArea }}</p>
<h2>Advanced AreaSelect (Level 2, All type)</h2>
<area-select type='all' :level='2' v-model="selectedProvinceCityArea" :data="pcaa"></area-select>
<p>Selected (Advanced): {{ selectedProvinceCityArea }}</p>
</div>
`
};