Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

vue组件中使用外部js方法

<div class="notic">{{ starPhone(phone) }}</div>
import { starPhone } from '@/utils/afterData'

utils/afterData

`export function starPhone(phoneNum) {
const start = phoneNum.slice(0, 3)
const end = phoneNum.slice(-4)
return ${start}****${end}
}`
报错
image.png
在methods中可以掉用starPhone()方法

如果在methods中写一个starPhone方法 页面中可以直接掉用,为什么引入的js方法在组件页面中不能直接使用呢

在methods中挂载一下方法
image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

如果要在template中使用变量或是函数,请先注册到data或是methods中:

<template>
    <div>{{title}} {{resolve(title)}}</div>
</template>

<script>
    export default {
        data() {
            return {
                title: '...'
            };
        },
        methods: {
            resolve(title) {
                return title;
            }
        }
    };
</script>

望采纳


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...