Skip to content

Commit fe5dc19

Browse files
hjkcaiktsn
authored andcommitted
Use class name as Vue component's name (#76)
* use class name as vue component's name * add name tests * fix test error caused by relative path in webpack config Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema. - configuration.output.path: The provided value "./test" is not an absolute path!
1 parent 34ef0ce commit fe5dc19

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function componentFactory (
2626
Component: VueClass,
2727
options: ComponentOptions<any> = {}
2828
): VueClass {
29-
options.name = options.name || (Component as any)._componentTag
29+
options.name = options.name || (Component as any)._componentTag || (Component as any).name
3030
// prototype props.
3131
const proto = Component.prototype
3232
Object.getOwnPropertyNames(proto).forEach(function (key) {

test/test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,34 @@ describe('vue-class-component', () => {
114114
expect(c.b).to.equal(3)
115115
})
116116

117+
describe('name', () => {
118+
it('via name option', () => {
119+
@Component({ name: 'test' })
120+
class MyComp extends Vue {}
121+
122+
const c = new MyComp()
123+
expect(c.$options.name).to.equal('test')
124+
})
125+
126+
it('via _componentTag', () => {
127+
@Component
128+
class MyComp extends Vue {
129+
static _componentTag = 'test'
130+
}
131+
132+
const c = new MyComp()
133+
expect(c.$options.name).to.equal('test')
134+
})
135+
136+
it('via class name', () => {
137+
@Component
138+
class MyComp extends Vue {}
139+
140+
const c = new MyComp()
141+
expect(c.$options.name).to.equal('MyComp')
142+
})
143+
})
144+
117145
it('other options', (done) => {
118146
let v: number
119147

test/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
'./test/test-babel.js'
55
],
66
output: {
7-
path: './test',
7+
path: __dirname,
88
filename: 'test.build.js'
99
},
1010
module: {

0 commit comments

Comments
 (0)