VueJS,无法在同一方法内访问“ this”或“ self”

时间:2019-03-13 17:21:16

标签: javascript api vue.js methods axios

目前,我在一个问题上坚持了几个小时。 可悲的是,我还没有通过谷歌搜索找到一个可行的解决方案。

问题是,从api中提取数据后,我无法在同一函数内访问“ self”或“ this”变量。我刚得到一个“未定义”。

getInvoice(){
  var self = this
  axios
    .get(url)
    .then(response => (self.invoice = response.data.data))

  console.log(self.invoice.invoice_nr) //undefined
},

最好的问候和预先的感谢。

1 个答案:

答案 0 :(得分:1)

首先,这不是Vue JS问题。

无论您使用什么框架,其他API都在Javascript中是异步的。

现在,为了从G:\Documents\workspace\mana\vendor\doctrine\dbal\lib\Doctrine\DBAL\DBALException.php:172 {▶} G:\Documents\workspace\mana\vendor\doctrine\dbal\lib\Doctrine\DBAL\DBALException.php:145 {▶} G:\Documents\workspace\mana\vendor\doctrine\dbal\lib\Doctrine\DBAL\Connection.php:911 {▶} G:\Documents\workspace\mana\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Exec\SingleSelectExecutor.php:50 {▶} G:\Documents\workspace\mana\vendor\doctrine\orm\lib\Doctrine\ORM\Query.php:334 {▶} G:\Documents\workspace\mana\vendor\doctrine\orm\lib\Doctrine\ORM\AbstractQuery.php:967 {▶} G:\Documents\workspace\mana\vendor\doctrine\orm\lib\Doctrine\ORM\AbstractQuery.php:922 {▶} G:\Documents\workspace\mana\vendor\doctrine\orm\lib\Doctrine\ORM\AbstractQuery.php:765 {▶} G:\Documents\workspace\mana\src\Repository\UserRepository.php:24 {▶} G:\Documents\workspace\mana\src\Security\LoginFormAuthenticator.php:74 {▶} G:\Documents\workspace\mana\vendor\symfony\security-guard\Provider\GuardAuthenticationProvider.php:102 {▶} G:\Documents\workspace\mana\vendor\symfony\security-guard\Provider\GuardAuthenticationProvider.php:96 {▶} G:\Documents\workspace\mana\vendor\symfony\security-core\Authentication\AuthenticationProviderManager.php:76 {▶} G:\Documents\workspace\mana\vendor\symfony\security-guard\Firewall\GuardAuthenticationListener.php:130 {▶} G:\Documents\workspace\mana\vendor\symfony\security-guard\Firewall\GuardAuthenticationListener.php:82 {▶} G:\Documents\workspace\mana\vendor\symfony\security-bundle\Debug\WrappedListener.php:46 {▶} G:\Documents\workspace\mana\vendor\symfony\security-bundle\Debug\TraceableFirewallListener.php:35 {▶} G:\Documents\workspace\mana\vendor\symfony\security-http\Firewall.php:90 {▶} G:\Documents\workspace\mana\vendor\symfony\security-bundle\EventListener\FirewallListener.php:48 {▶} G:\Documents\workspace\mana\vendor\symfony\event-dispatcher\Debug\WrappedListener.php:115 {▶} G:\Documents\workspace\mana\vendor\symfony\event-dispatcher\EventDispatcher.php:212 {▶} G:\Documents\workspace\mana\vendor\symfony\event-dispatcher\EventDispatcher.php:44 {▶} G:\Documents\workspace\mana\vendor\symfony\event-dispatcher\Debug\TraceableEventDispatcher.php:145 {▶} G:\Documents\workspace\mana\vendor\symfony\http-kernel\HttpKernel.php:126 {▶} G:\Documents\workspace\mana\vendor\symfony\http-kernel\HttpKernel.php:67 {▶} G:\Documents\workspace\mana\vendor\symfony\http-kernel\Kernel.php:198 {▶} G:\Documents\workspace\mana\public\index.php:25 {▶} 访问数据,您需要将其添加到Promise函数中。

看看Axios Docs

Promises

要获取数据,

.then

希望这会有所帮助!